Clear Filters
Clear Filters

USRP N210 rx() returning data vector with different length

2 views (last 30 days)
I am trying to use the USRP N210 to record rawdata from GNSS. My hardware configuration uses the USRP N210+UBX40+GPSDO Kit. I am trying to record data binary data into a file with the following code:
%% collect gnss rf data
filename = 'USRPN210_test.dat';
fc = 1575.42e6; % center frequency
gain = 30; % tuner gain
decfac = 8; % 4 to record a 25MHz e 8 para gravar a 12.5MHz
mclkr = 100e6;
fs = mclkr/decfac; % sampling frequency given by mclkr/decfac 2.5MHz
framelen = fs/100; % samples per frame
datatype_rtl = 'int8';
dt = 60; %300;%duration in seconds
rx = comm.SDRuReceiver('Platform','N200/N210/USRP2','IPAddress','192.168.10.2', 'PPSSource','GPSDO','ClockSource','GPSDO','CenterFrequency',fc,...
'SamplesPerFrame',framelen,'MasterClockRate',mclkr,'DecimationFactor',decfac,'Gain',gain, 'ChannelMapping', 1);
rx(); % discard first packet
nframes = dt*(fs/framelen);
totaldata = nframes*framelen;
fprintf('Collecting data from USRP N210...\n')
%% save data to file
fid = fopen(filename,'w');
data = int16(zeros(framelen*2,1));
if fid~=-1
dataread = 0;
TotalOverrun = 0;
while dataread<totaldata
[rawdata,len,overrun,timeStamps] = rx();
fprintf("Rx length: %d\t", len);
if(len > 0)
data(1:2:2*len) = real(rawdata);
data(2:2:2*len) = imag(rawdata);
TotalOverrun = TotalOverrun + overrun;
dataread = dataread + double(len);
fprintf('time = %.2f done %.2f%% Overrun: %i TimeStamps %i\n',dataread/fs,100*dataread/totaldata,TotalOverrun,timeStamps(1));
fwrite(fid,data(1:2*len),datatype_rtl);
else
a=1;
end
end
fprintf('\n');
release(rx);
fclose(fid);
end
If I run the code above, for some iterations there is no issue, but after a while an error happens due to a different size being outputed from the rx(). Shouldn't the rx() function always return the specified number of samples using the 'SamplesPerFrame' parameter of the comm.SDRuReceiver?
Collecting data from USRP N210...
Rx length: 125000 time = 0.01 done 0.02% Overrun: 0 TimeStamps 7.482554e-02
Rx length: 125000 time = 0.02 done 0.03% Overrun: 0 TimeStamps 8.482554e-02
Rx length: 125000 time = 0.03 done 0.05% Overrun: 0 TimeStamps 9.482554e-02
Rx length: 125000 time = 0.04 done 0.07% Overrun: 0 TimeStamps 1.048255e-01
Rx length: 125000 time = 0.05 done 0.08% Overrun: 0 TimeStamps 1.148255e-01
Rx length: 125000 time = 0.06 done 0.10% Overrun: 0 TimeStamps 1.248255e-01
Rx length: 125000 time = 0.07 done 0.12% Overrun: 0 TimeStamps 1.348255e-01
Rx length: 125000 time = 0.08 done 0.13% Overrun: 0 TimeStamps 1.448255e-01
Rx length: 125000 time = 0.09 done 0.15% Overrun: 0 TimeStamps 1.548255e-01
Rx length: 125000 time = 0.10 done 0.17% Overrun: 0 TimeStamps 1.648255e-01
Rx length: 51590 Unable to perform assignment because the left and right sides have a different number of elements.
Error in readUSRPN210 (line 31)
data(1:2:2*len) = real(rawdata);

Answers (1)

MULI
MULI on 18 Sep 2024
Hi Diogo,
The error you are encountering happens when the number of samples returned by `rx() (i.e., `len) is not consistent with the expected `SamplesPerFrame.
You can handle variable-length outputs from the `rx() function by following below suggestions:
1. Dynamic Data Handling: Adjust your code to accommodate varying lengths of data. Instead of assuming a fixed length, adjust the array size based onlen
data(1:2:2*len) = real(rawdata(1:len));
data(2:2:2*len) = imag(rawdata(1:len));
2. Check for Buffer Overruns: If the system is losing data, it might be overloaded. Try increasing decfac or reducing framelen to make data processing easier. For more information and examples related to overcome the overruns you can refer to the following MathWorks documentation link https://www.mathworks.com/help/releases/R2022b/supportpkg/usrpradio/ug/comm.sdrureceiver-system-object.html
3. Adjust Sampling Rate: If the system is struggling, reduce the sampling rate by increasing `decfac. This decreases the data processed per second, giving the system more time to handle each frame.
4. Error Handling for Mismatched Sizes: Add a check for unexpected frame sizes to prevent errors:
if len > 0 && len == framelen
data(1:2:2*len) = real(rawdata);
data(2:2:2*len) = imag(rawdata);
else
warning('Unexpected frame size: %d', len);
end
These steps will help you to manage variable frame sizes and improve data collection stability.

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!