pause() function is inaccurate
17 views (last 30 days)
Show older comments
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?)
0 Comments
Accepted Answer
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
Steven Lord
on 10 Aug 2022
The pause function wasn't inaccurate (in the way you described it.) You expected that the pause would only start once the sound call finished playing, but it actually started once the sound call finished executing (while your computer continued to play the sound.)
2 seconds of sound plus 4 seconds of pause (silence) indicate the correct duration of one execution of the body of the for loop is 6 seconds.
If you want the total sound + silence time to be 4 seconds just update the input to pause based on how long your sound lasts. The TotalSamples and SampleRate properties of the audioplayer object may be useful to you if you choose to do this. Or you could measure the actual execution time of playblocking and calculate the pause duration from that.
More Answers (1)
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)
t1 = tic;
while toc(t1) <= 4
end
toc(t1)
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
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!