pause() function is inaccurate

10 views (last 30 days)
Joanne Hall
Joanne Hall on 10 Aug 2022
Edited: Joanne Hall on 10 Aug 2022
Dear MATLAB,
Using a function to produce a series of auditory click trains. It does produce the click trains, but the matlab pause() function is inaccurate. it reports (roughly) 4 seconds but the duration is in fact more like 2 seconds. Below is the pasted code...
*****************************************
close all; clear; clc;
% [train, t, fs] = hb_clickTrain( frq, dura, width, fs, plotOption )
[train, t, fs] = hb_clickTrain(40,2,0.001,48000,1);
trials = 5;
tstamp = zeros(trials,6);
for ci = 1:trials
sound(train,fs)
c= clock;
tstamp(ci,:,:,:,:,:,:) = c;
tic
pause(4)
toc
end
**************************************
I hear roughly 2 seconds of pause, but matlab prints out and saves...
Elapsed time is 4.005545 seconds.
Elapsed time is 4.003628 seconds.
Elapsed time is 4.003640 seconds.
Elapsed time is 4.007986 seconds.
Elapsed time is 4.013071 seconds.
Any advice is greatly appreciated!
Thanks in advance,
Joanne
(is there an alternative to the pause function?)

Accepted Answer

Steven Lord
Steven Lord on 10 Aug 2022
You probably want to create an audioplayer object and call playblocking on it instead of using sound.
load handel
sound(y, Fs);
tic
pause(1)
toc
If you run that code as a block, the message from the toc call will appear while the sound is still playing.
p = audioplayer(y, Fs);
playblocking(p);
tic
pause(1)
toc
If you run this block of code, the message from the toc call will appear a second after the playback has concluded.
  5 Comments
Joanne Hall
Joanne Hall on 10 Aug 2022
Edited: Joanne Hall on 10 Aug 2022
OoOohhhh I get it now! Thanks for that insight!
Yes I was thinking one funnction has to finish executing before the next funciton starts (not that they can be at the same time. I wonder if thats exclusive to the for loop

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 10 Aug 2022
Edited: Bruno Luong on 10 Aug 2022
The accuracy is documented.
"The accuracy of the pause function is subject to the scheduling resolution of your operating system, and to other concurrent system activity. The accuracy is not guaranteed, and finer resolution results in higher relative error."
If you want better accuracy,just use tic toc
t1 = tic;
while toc(t1) <= 4
pause(0);
end
toc(t1)
Elapsed time is 4.001736 seconds.
t1 = tic;
while toc(t1) <= 4
end
toc(t1)
Elapsed time is 4.001507 seconds.
This is however will take spin the CPU.
NOTE: the accuracy is better on my PC (Elapsed time is 4.000097 seconds.) than on online server
  1 Comment
Joanne Hall
Joanne Hall on 10 Aug 2022
Thanks very much for your answer! I tried it, but the problem is that it does not actually pause for 4 seconds, like more like 1-2 seconds in the for loop. But it documents 4.000149 seconds, for ex.
so, I hear the clicks for 2 seconds, pause for 1-2 seconds (instead of 4), click train again for 2 seconds.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!