Examples

You can download all nirfsa examples for latest version here

nirfsa_getting_started_iq.py

 1import argparse
 2import nirfsa
 3import numpy as np
 4import sys
 5
 6
 7def example(resource_name, options, iq_carrier_frequency, reference_level, number_of_samples):
 8    with nirfsa.Session(resource_name=resource_name, id_query=False, reset_device=False, options=options) as rfsa_session:
 9        # Configurations
10        rfsa_session.acquisition_type = nirfsa.AcquisitionType.IQ
11
12        rfsa_session.reference_level = reference_level
13        rfsa_session.iq_carrier_frequency = iq_carrier_frequency
14        rfsa_session.number_of_samples = number_of_samples
15
16        iq_data_array = np.zeros(number_of_samples, dtype=np.complex128)
17        wfm_info = rfsa_session.read_iq_single_record_into(iq_data_array)
18
19        # Do something useful with the data.
20        # We will present average power: 10log(((I^2 + Q ^2) / 2R) * 1000), where
21        # R = 50 Ohms.
22        samples = np.asarray(wfm_info.samples)
23        accumulator = 0.0
24        if len(samples) > 0:
25            for sample in samples:
26                magnitude_squared = sample.real * sample.real + sample.imag * sample.imag
27                # we need to handle this because log(0) return a range error.
28                if magnitude_squared == 0.0:
29                    magnitude_squared = 0.00000001
30                accumulator += 10.0 * np.log10((magnitude_squared / (2.0 * 50.0)) * 1000.0)
31            print('Average power = %0.1f dBm' % (accumulator / len(samples)))
32
33
34def _main(argsv):
35    parser = argparse.ArgumentParser(description='Acquires IQ data using NI-RFSA.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
36    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of the NI RF signal analyzer.')
37    parser.add_argument('-c', '--iq-carrier-frequency', default=1e9, type=float, help='IQ carrier frequency in Hz.')
38    parser.add_argument('-r', '--reference-level', default=0.0, type=float, help='Reference level in dBm.')
39    parser.add_argument('-s', '--number-of-samples', default=1024, type=int, help='Number of IQ samples to acquire.')
40    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string for the session.')
41    args = parser.parse_args(argsv)
42    example(args.resource_name, args.option_string, args.iq_carrier_frequency, args.reference_level, args.number_of_samples)
43
44
45def main():
46    _main(sys.argv[1:])
47
48
49def test_example():
50    options = {'simulate': True, 'driver_setup': {'Model': '5841', }, }
51    example('simulated5841', options, 1e9, -10.0, 1024)
52
53
54def test_main():
55    cmd_line = ['--resource-name', 'simulated5841', '--iq-carrier-frequency', '1e9', '--reference-level', '-10', '--option-string', 'Simulate=1, DriverSetup=Model:5841']
56    _main(cmd_line)
57
58
59if __name__ == '__main__':
60    main()

nirfsa_getting_started_spectrum.py

 1import argparse
 2import nirfsa
 3import numpy as np
 4import sys
 5
 6
 7def example(resource_name, options, center_frequency, span, reference_level):
 8    with nirfsa.Session(resource_name=resource_name, id_query=False, reset_device=False, options=options) as rfsa_session:
 9        # Configurations
10        rfsa_session.acquisition_type = nirfsa.AcquisitionType.SPECTRUM
11        rfsa_session.reference_level = reference_level
12        rfsa_session.resolution_bandwidth = 10e3
13        rfsa_session.configure_spectrum_frequency(center_frequency=center_frequency, span=span)
14
15        spectrum_buffer = np.zeros(rfsa_session.number_of_spectral_lines, dtype=np.float64)
16
17        spectrum_info = rfsa_session.read_power_spectrum_into(spectrum_buffer, timeout=10.0)
18
19        # Do something useful with the data.
20        # We will find the highest peak in a bin, which is not the actual highest
21        # peak and frequency we could find in the acquisition. For an accurate
22        # peak search, we can analyze the data with the Spectral Measurements Toolset.
23        samples = np.asarray(spectrum_info.samples)
24        greatest_peak_index = int(np.argmax(samples))
25        greatest_peak_power = samples[greatest_peak_index]
26        greatest_peak_frequency = spectrum_info.initial_frequency + spectrum_info.frequency_increment * greatest_peak_index
27
28        print(
29            'The highest peak in a bin is %0.1f dBm at %0.3f MHz.'
30            % (greatest_peak_power, greatest_peak_frequency / 1e6)
31        )
32
33
34def _main(argsv):
35    parser = argparse.ArgumentParser(description='Acquires a power spectrum using NI-RFSA.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
36    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of the NI RF signal analyzer.')
37    parser.add_argument('-c', '--center-frequency', default=1e9, type=float, help='Center frequency in Hz.')
38    parser.add_argument('-s', '--span', default=100e6, type=float, help='Span in Hz.')
39    parser.add_argument('-r', '--reference-level', default=0.0, type=float, help='Reference level in dBm.')
40    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string for the session.')
41    args = parser.parse_args(argsv)
42    example(args.resource_name, args.option_string, args.center_frequency, args.span, args.reference_level)
43
44
45def main():
46    _main(sys.argv[1:])
47
48
49def test_example():
50    options = {'simulate': True, 'driver_setup': {'Model': '5841', }, }
51    example('simulated5841', options, 1e9, 100e6, -10.0)
52
53
54def test_main():
55    cmd_line = ['--resource-name', 'simulated5841', '--center-frequency', '1e9', '--span', '100e6', '--reference-level', '-10', '--option-string', 'Simulate=1, DriverSetup=Model:5841']
56    _main(cmd_line)
57
58
59if __name__ == '__main__':
60    main()