Create a infinite while loop

1 view (last 30 days)
Miguel Albuquerque
Miguel Albuquerque on 22 Jun 2022
Commented: dpb on 22 Jun 2022
Hey guys thanks in advance.
I have a code that reads samples from a hardware receiver. I want that the hardware keeps receiving until I press ctrl+c.But I need to write to a .txt file the samples and the time it ocurred. Is it better to do it after the while loop as I have or in between?
So the code I have, it starts the hardware to start receiving, then records the date time and between the while loop is the samples receiving.
% Start the module
dev.start();
fprintf('Start of LimeSDR\n');
tic;
start_time=datetime('now','Format','dd-MMM-uuuu HH:mm:ss.SSS');
while true
% Receive samples on RX1 channel
indRx1 = 1; % index of the last received sample
[samples1, ~, samplesLength1] = dev.receive(Fs*Ts,1);
bufferRx1(indRx1:indRx1+samplesLength1-1) = samples1;
end
% Cleanup and shutdown by stopping the RX stream and having MATLAB delete the handle object.
dev.stop();
tempo_rececao=toc;
stop_time=datetime('now','Format','dd-MMM-uuuu HH:mm:ss.SSS');
clear dev;
fprintf('Stop of LimeSDR\n');
TT=array2timetable(samples1,'SampleRate',Fs,'StartTime',start_time);
writetimetable(TT,'Timetable_Rx1.txt','Delimiter','bar');

Accepted Answer

dpb
dpb on 22 Jun 2022
Edited: dpb on 22 Jun 2022
As is, it would be better to open the file first and write each record; the line
bufferRx1(indRx1:indRx1+samplesLength1-1) = samples1;
where bufferRx1 has not been preallocated will cause memory reallocation to augment its size on every pass through the loop. This won't be terribly noticeable for a short time frame if the amount of data isn't very large, but will begin to noticeably impact time as the acquisition proceeds.
  2 Comments
dpb
dpb on 22 Jun 2022
'Pends on the form of the data -- it would be best to use lower-level i/o functions for a continuous acquistion instead of the overhead of the higher-level routines that open/close the file on every write -- that also has a lot of overhead associated with it.
But, we don't know the form of the data in either type nor number of samples/channels/etc.... to be able to write precise code.
Your buffer is just appending onto the end of what appears to be a vector; if so, then probably the lowest overhead option would be to insert an fopen first to a file for write before starting the device/acquisition, then read the start time and begin the acquisition. If you don't have some other restriction on the file having to be human-readable, then unformatted ("binary') output with frwrte iwould be fastest for recording the data; you can then post-process that file to create the time table for convenience.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by