필터 지우기
필터 지우기

pause() function is inaccurate

조회 수: 12 (최근 30일)
Joanne Hall
Joanne Hall 2022년 8월 10일
편집: Joanne Hall 2022년 8월 10일
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?)

채택된 답변

Steven Lord
Steven Lord 2022년 8월 10일
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
Steven Lord
Steven Lord 2022년 8월 10일
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 2022년 8월 10일
편집: Joanne Hall 2022년 8월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2022년 8월 10일
편집: Bruno Luong 2022년 8월 10일
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
Joanne Hall
Joanne Hall 2022년 8월 10일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 AI for Audio에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by