I'm writing to notify list readers of a problem
playing audio using Matlab. |
The problem is that brief signals are truncated, and it happens using any of the common commands for playing audio (wavplay, sound, and the play method of the audioplayer object).
I can't specify the exact conditions under which the truncation occurs. However, with a sample rate of 44.1 kHz, signals shorter than about 500 msec are truncated. I haven't looked much at other sample rates.
A simple work-around for the problem is to pad the end of the signal with a sufficient number of zeros that truncation occurs during the zero segment. I've found that at least 2000 zeros need to be added to assure no truncation of a 10-msec signal (with a sample rate of 44.1kHz).
I've brought this problem to the attention of The Mathworks. They confirmed the problem, which they attribute to a bug in their driver.
The Mathworks also informed me that the problem has been solved in their latest release (R2011b) for playing using audioplayer, but not necessarily for the other audio commands. I've confirmed this in very limited tests.
The following is a piece of code that you can use to observe the truncation. It creates a 100-msec, 1-kHz sine wave, applies a hanning window, and then plays it first with zero-padding and then without. You should be able to hear the click at the end of the second tone, which is not present on the first. If not, you can see it on an oscilloscope.
fs = 44100;
f = 1000;
T = 0.1;
nPad = 2000;
nS = round(fs*T);
y = sin(2.*pi*f/fs*(1:nS)');
w = .5*(1 - cos(2*pi*(1:nS)'/(nS+1)));
y = w.*y;
yPad = [y; zeros(nPad,1)];
wavplay(yPad, fs, 'sync')
wavplay(y, fs, 'sync')
-- Patrick M. Zurek President Sensimetrics Corporation 14 Summer St. Suite 403 Malden, MA 02148 Tel: 781-399-0858 x237 Fax: 781-399-0853 email: pat@xxxxxxxx web: www.sens.com