Simulate a monitoring system from data stored in text file

3 次查看(过去 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

回答(2 个)

Walter Roberson
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

Vincent Thevenot
Vincent Thevenot 2015-10-11
Hello Walter,
thank you very much for your help. It works fine. But the period of the timer function is limited to 0.001 s. In my project the minimum sample frequency is 12800 Hz, so it gives a period of 0.000078125 s. So the simulation is too slow, mainly if I want to perform calculus (RMS, spectrum, threshold detection, ...). And in fact, I have 6 different signals with correlation between them, and the duration of the measurement can be very big (several hours) :-)
I'm wondering if Simulink can be the solution. There is also LabView, but I'm a little scared about the integration of LabView with Matlab script. The simulation is just to test my detection algorithms.
Do you have an idea about that ? What is your point of view ?
Thanks in advance
Vincent
  1 个评论
Walter Roberson
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by