Timer with very precise period time

29 ビュー (過去 30 日間)
Butterflyfish
Butterflyfish 2021 年 11 月 30 日
編集済み: Jan 2021 年 12 月 13 日
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
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
Adam Danz 2021 年 12 月 10 日
Note from the documentation,
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
Jan 2021 年 12 月 10 日
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 件のコメント
Butterflyfish
Butterflyfish 2021 年 12 月 13 日
Thanks all for your thoughts and comments! How do you make the C-mex function work (the HAT)?
Jan
Jan 2021 年 12 月 13 日
編集済み: Jan 2021 年 12 月 13 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by