Timer with very precise period time
이전 댓글 표시
I have a script that plays a sound every 15 minutes a given number of times. It is triggered at a given datetime. As I would like to use it to calculate the clock drift of a recorder, I need it to be extremely precise. The following script works but is not very precise on the period time (15 minutes +/- some milliseconds). Is there a way to link it with .NET times or any other more precise protocol?
fTime = datetime('30-Nov-2021 13:00:00.000');
period = 15 * 60;
n = 3;
fs=41000;
click=sin(2*pi*10000/fs + pi);
t = timer('StartDelay', 0, 'Period', period, 'TasksToExecute', n, 'ExecutionMode', 'FixedRate');
t.StartFcn = @(~,thisEvent)disp([thisEvent.Type ' executed '...
datestr(thisEvent.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
t.StopFcn = @(~,thisEvent)disp([thisEvent.Type ' executed '...
datestr(thisEvent.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF')]);
t.TimerFcn = {@PlaySound, click};
startat(t,fTime)
function PlaySound(obj, event, x)
sound(x)
event_time = datestr(event.Data.time,'dd-mmm-yyyy HH:MM:SS.FFF' );
disp (['Sound played at ' event_time])
end
댓글 수: 2
Eric Sofen
2021년 12월 10일
It looks like you're using timer correctly. This advice on using drawnow to avoid timer delays might be helpful.
What precision do you need and what precision are you getting (and how are you determining it)?
Adam Danz
2021년 12월 10일
The timer object is subject to the limitations of your hardware, operating system, and software. Avoid using timer objects for real-time applications. If MATLAB is busy processing another task, the timer callback might not execute.
답변 (1개)
Jan
2021년 12월 10일
1 개 추천
Under ideal conditions a high accuracy timer of the operating system inside a C-mex function. See e.g. FEX: HAT . But remember, that in the real life, the timer may be accurate, but triggering the sound can be delayed again. Sometimes even some updates of the operating system causes delays in the mouse movement.
A stable method to create a sound with certain intervals is to produce a continous sound with corresponding phases of silence and to play it on a real-time system. A cheap version is a CD, DVD or BlueRay player. This wil much much simpler and more stable than using a mulithreaded multipurpose computer, which adjusts the CPU frequency dynamically.
댓글 수: 6
Walter Roberson
2021년 12월 10일
Also, do not use sound() for real-time work. See Audio System Toolbox audioDeviceWriter() https://www.mathworks.com/help/audio/ref/audiodevicewriter-system-object.html
I've discussed with colleagues about the problem. The problem is serious. CD and DVD players have a limited accuracy also. If you burn a sound signal with a frequency of 440Hz on a CD, the player will not reproduce 440.0000Hz. You need a precision of a millisecond over 15 minutes (1 to 900'000 ms). I cannot find a statement in the documentation of my CD player, which defines the accuracy of the output frequency. Therefore I would not trust this equipment. At least a CD player will not have delays in the magnitude of seconds, which can happen with all general purpose operating systems. Using a high-level language like Matlab increase the jitter massively.
In other words: You cannot control the time drift of a recorder with a signal generator, which has an uncontrolled time drift.
You can run a microcontroller or a computer with a real time OS. Both need a stabilised internal clock. We've used some hardware timers with a precision of nanoseconds, but only over a period of 2 seconds.
Walter Roberson
2021년 12월 11일
NTP (network time protocol) can maintain less than 1 millisecond, but only under ideal conditions.
However, there is the possibility of PTP (Precision Time Protocol) https://www.masterclock.com/support/library/network-timing-ntp-vs-ptp
Jan
2021년 12월 12일
Using a precise timer, you can trigger the playing of a sound synchronsously and check afterwards, if there was an unexpected delay. If this the delay is negligible, the controlling of the devices internal clock can be successful. This is not guaranteed to work in all cases, but there can be successful runs. This might be enough to solve the problem of the OP.
Butterflyfish
2021년 12월 13일
Install a compiler: https://www.mathworks.com/support/requirements/supported-compilers.html
Then follow the instructions found in hat.c:
* compile command for Windows (needs Windows SDK)
* mex -O hat.c
* compile command for Linux
* mex -O hat.c -lrt
Using the high accuracy timer inside the MEX function, but triggeting the sound in Matlab, causes some delays and an additional jitter. Prefer to start the sound inside the C-mex function and to check the HAT after the playing again.
카테고리
도움말 센터 및 File Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!