pause() function is inaccurate

7 visualizaciones (últimos 30 días)
Joanne Hall
Joanne Hall el 10 de Ag. de 2022
Editada: Joanne Hall el 10 de Ag. de 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?)

Respuesta aceptada

Steven Lord
Steven Lord el 10 de Ag. de 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 comentarios
Steven Lord
Steven Lord el 10 de Ag. de 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.
Joanne Hall
Joanne Hall el 10 de Ag. de 2022
Editada: Joanne Hall el 10 de Ag. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Bruno Luong
Bruno Luong el 10 de Ag. de 2022
Editada: Bruno Luong el 10 de Ag. de 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 comentario
Joanne Hall
Joanne Hall el 10 de Ag. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre AI for Audio en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by