Post Go back to editing

About sdr.tx(). Can I change the transmitter frequency after call this function?

Category: Software
Product Number: Pluto
Software Version: v0.34

Hi everyone,
I am trying to schedule the transmitting frequency of the Pluto
For example, by scheduling in python, I would like to change the frequency from 900,1 MHz to 900,2 MHz and 900,3MHz automatically at a specific time using the schedule library python.
I have tried to set the lo_frequency at 900 MHz and created a single-tone signal at 100KHZ, which means the signal will transfer a single tone at 900,1MHz. Then I tried to change the frequency of the signal to 200Khz but it seem that the transmitter did not switch to the new frequency.
Can anyone show me how to change the frequency of the transmitter in python. Thanks 

import numpy as np
import adi
import matplotlib.pyplot as plt
import time 
import schedule

#define parameter
sample_rate = 2e6
ts = float(1/sample_rate)
center_freq = 900e6
num_samples = 1024

#declare hardware object with ip address
sdr = adi.Pluto("ip:192.168.2.1")
sdr.sample_rate = int(sample_rate)

# config TX
sdr.tx_lo = int(center_freq)
sdr.tx_rf_bandwidth = int(sample_rate)
sdr.rx_hardwaregain_chan0 = -90 #dB

#config rx
sdr.rx_lo = int(center_freq)
sdr.rx_rf_bandwidth = int(sample_rate)
sdr.gain_control_mode_chan0 = 'manual'
sdr.rx_hardwaregain_chan0 = -90 #dB
sdr.rx_buffer_size = num_samples


def create_signal(freq): ## Create a sinewave waveform with fc = freq
    print("fuction called")
    fs = int(sample_rate)
    fc = int(freq / (fs / num_samples)) * (fs / num_samples)
    ts = 1 / float(fs)
    t = np.arange(0, num_samples * ts, ts) #Return evenly spaced values within a given interval.
    i = np.cos(2 * np.pi * t * fc) * 2 ** 14
    q = np.sin(2 * np.pi * t * fc) * 2 ** 14
    samples = i + 1j * q
    return samples
samples = create_signal(100000)

# turn on buffer to transmit overlapping
# sdr.tx_cyclic_buffer = True
# sdr.tx(samples) #transmit the samples

fig, ax = plt.subplots()
freq_vector = np.fft.fftshift(np.fft.fftfreq(num_samples, d=ts))
line, = ax.plot(freq_vector, np.random.rand(num_samples),'-', lw=2)
ax.set_xlabel('Frequency')
ax.set_ylabel('Magnitude dB')
ax.set_ylim(-80,60)
fig.show()
while True:
    #Receiving samples
    rx_samples = sdr.rx() # this will return received IQ samples into rx_samples
    rx_fft = np.fft.fftshift(np.fft.fft(rx_samples))
    mag_dbfs = 20*np.log10((np.abs(rx_fft))/(2**12))
    line.set_ydata(mag_dbfs)
    fig.canvas.draw()
    fig.canvas.flush_events()

    



  • Hi,

    You need to create a new signal using the new frequency and send it to Pluto using the sdr.tx(new_signal). I see the commented area on the tx generation and you set the TX to cyclic. This means that you need to destroy the existing buffer(at 100KHz) before pushing the new one(at 200KHZ). Did you get an error regarding this cyclic buffer when trying to transmit the new signal at 200KHz?

    The TX signal generation code could look something like this - if you pause after each TX generation and acquire the RX samples, you would see the frequency of the signal changing.

    wanted_freq = 100000
    sdr.tx_cyclic_buffer = True
    for i in range(10):
        samples = create_signal(wanted_freq * i)
        # turn on buffer to transmit overlapping
        sdr.tx_destroy_buffer()
        sdr.tx(samples) #transmit the samples
        
       #acquire the RX buffers as needed


    Can you try this out and check if it works for you?

    Thank you!
    -Alexandra

  • Thank you Alexandra,
    You are right, when I tried to transmit the new samples without destroying the buffer. It raised an error, then whenever I put new samples into the sdr.tx() function, I have to destroy the buffer first to make sure it will work.