0

I have a code in MATLAB

function [freqOffset] = find_frequency_offset(rxData, sample_rate)
    magnitudeFFT = (abs(fft(rxData))).^2;           % Magnitude FFT
    FFTSize = length(magnitudeFFT);
    FFTSig = fftshift(magnitudeFFT);
    [~, maxIdx] = max(FFTSig);                          % Find index of max magnitude
    %disp(FFTSize);
    %disp(maxIdx);
    offsetIdx = maxIdx-1-FFTSize/2;                     % Convert to index symmetric with 0
    %disp(offsetIdx);
    Ts = 1 / sampleRate;                               % Sample time
    delta_f = 1/Ts/FFTSize;                             % Frequency resolution of FFT
    freqOffset = offsetIdx*delta_f;
end
center_freq = 868e;
sample_rate = 2e6;
num_samples = 2e6;
device_name = "Pluto";
MAX = 100;
receiver_gain = 0;
sdrReceiver = sdrrx(device_name);
sdrReceiver.RadioID = 'usb:0';
sdrReceiver.BasebandSampleRate = sample_rate;
sdrReceiver.CenterFrequency = center_freq;
sdrReceiver.SamplesPerFrame = num_samples;
sdrReceiver.Gain = receiver_gain;
sdrReceiver.OutputDataType = 'double';
sdrReceiver.EnableBasebandDCTracking = false;          
sdrReceiver.EnableRFDCTracking = false;                
sdrReceiver.EnableQuadratureTracking = false; 

while(countere<=MAX)
    rxData = sdrReceiver();
    freqOffset = find_frequency_offset(rxToneData, sample_rate)
    if(freqOffset~=0)
        disp(freqOffset)
    else
        disp(Tone is not found);
    counter = counter + 1;
end

Then i rewrite this code on Python

import adi
import commpy
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, fftshift

sampleRate = 2e6
centerFreq = 868e6
sdr = adi.Pluto("ip:192.168.2.1")
sdr.sample_rate = sampleRate

# Receiver
sdr.rx_lo = int(centerFreq)
sdr.rx_rf_bandwidth = int(sampleRate)
sdr.rx_buffer_size = int(sampleRate)
sdr.gain_control_mode_chan0 = 'manual'
counter = 0
sec = 0
while (counter < 100):
    received_data = sdr.rx()
    print(len(received_data))
    freq_offset = find_freq_offset(received_data, sampleRate)
    sec = time.time()
    struct = time.localtime(sec)
    print("Time is: ", time.strftime('%H:%M', struct))
    if (freq_offset != 0):
        print("Value of freq_offset is: ", freq_offset)
    else:
        print("Tone is not found")
    counter = counter + 1

When i run on Matlab it works correctly when signal is not found freq_offset is 0 But when i run it on python freq_offset is -67 or -78 although there is no any signal. I can't figure out why this happens. I think it's enablebasebanddctrackin when i set true result is same as on Python. But there is no such parameter in pyadi-iio

0 Answers0