Simulate a monitoring system from data stored in text file
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a data text file which contains a time vector (1st column) and an amplitude vector (2nd column).
I would like to read each row of this text file every 1/Fs second (Fs = sample rate of the text file), and apply some calculus in the same time (detect when the amplitude is greater than a threshold for example). The aim is to simulate a monitoring system from data stored in text file.
I have no idea to do that. I tried with the classical function used to read text file (fscanf, fread, ...) and to animate a plot (animatedline), but it's to slow. My sample rate can be 44.1 kHz, and the duration of the data can be several minutes (and maybe more) and it's not necesary that Matlab store the data in memory.
Does anyone have an idea how to perform that ?
Thank you in advance for your answers
Vincent
0 个评论
回答(2 个)
Walter Roberson
2015-10-10
function simulate_input
fid = fopen('YourFile.txt','rt');
if fid < 0; error('Could not find input file'); end
TimerFs = 44100;
t = timer('BusyMode', 'queue', 'ExecutionMode', 'FixedRate', 'Period', TimerFs, 'TimerFcn', @(src, event) ProcessOneLine(src, event, fid));
initialize some variables like the graphics
start(t)
end
function ProcessOneLine(src, event, fid);
input_line = fgetl(fid);
if ~ischar(input_line)
stop(src); %reached end of file, stop the timer
return;
end
simulated_input = sscanf(input_line, '%f%f');
respond_to_input(simulated_input);
end
Now this following section is the same code that you would call if you were running in real-time instead of simulated time. The parts above have to do with simulating real-time input, but once you have the input you would respond to it the same way as if you were "live"
function respond_to_input(time_thresh)
input_time = time_thresh(1);
input_threshold = time_thresh(2);
... do some calculations, maybe update the graphics
end
0 个评论
Vincent Thevenot
2015-10-11
1 个评论
Walter Roberson
2015-10-11
Good point about the minimum period of 0.001 .
Simulink's timer has a minimum period of 0.01 so it will not help you.
My previous research here suggests that timers are not going to get you what you want unless you call into C (and I'm not sure it would be possible to run MATLAB that fast anyhow.)
For more information on Windows HPET see https://en.wikipedia.org/wiki/High_Precision_Event_Timer (and note the drawbacks)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!