Create a infinite while loop
1 次查看(过去 30 天)
显示 更早的评论
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');
0 个评论
采纳的回答
dpb
2022-6-22
编辑:dpb
2022-6-22
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 个评论
dpb
2022-6-22
'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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!