MATLAB: Audioplayer function: clicks during playback of sine waves

audioplayer

I'm using audioplayer to generate sound from a series of sine waves (~200 – 500 Hz). My sample rate is 2KHz and each wave is played for 500ms. I'm presenting 16 waves, in series. However, the playback is riddled with audible clicks. These are not at the transition points between each consecutive wave. If I play the wave series (x) directly using 'sound(x, 2000)', I get no clicks. If I write the sounds to a .wav file, I get no clicks and the waveform looks clean in a sound file editor. The clicks are independent of output device also.

Best Answer

The analog output of a sample and hold system to a sine wave input is a sine wave multiplied by a pulse train convolved with a rectangular window. Multiplication in the time domain is convolution in the frequency domain. A sine wave is two pulses in the frequency domain and a pulse train is a pulse train. The period of the pulses is half the sample rate. The result of the convolution is then two pulses every half sample rate. You then scale everything by a sinc function.
EDIT
Lets see if MATLAB can clarify the above statements:
fs = 1e-6;
t = 0:fs:1;
f = 0:(1/(fs*length(t))):((1/fs)-(1/(fs*length(t))));
w = 2*pi*100;
sr = 2e3;
x = sin(w*t);
pt = false(size(t));
pt(1:(1/(sr*fs)):end) = true;
y = zeros(size(x));
y(pt) = x(pt);
y = conv(y, ones(1, 1/(sr*fs)), 'same');
subplot(2, 1, 1);
plot(t, y, t, x)
xlabel('t (s)')
ylabel('Amplitude (V)')
xlim([0, 10e-3])
subplot(2, 1, 2);
plot(f, 20*log10(abs(fft(y))), f, 20*log10(abs(fft(x))))
xlabel('f (Hz)')
ylabel('Amplitude (dB)')
xlim([0, 10*sr])
ylim([0, 100])
The variable x is a digital representation of an analog sinewave with a frequency of 100 Hz. The variable y is a digital representation of the analog output of a sample and hold sound card running at a sample rate of sr (2 kHz). The variable pt does the "sample" and the convolution does the "hold".
The magnitude of the FFT of x has a single peak at 100 Hz. There is also another peak at -100 Hz, which due to the magic of the DTFT (i.e., FFT) shows up at fs-100Hz. If you expand the axis you will see it.
xlim([0, 1e6])
The magnitude of the FFT of y has the same peak at 100 Hz (and at -100 Hz). It also has a train of double peaks at sr. This is the aliasing I am talking about. There is no sample rate for which you get no aliasing. For auditory signals a sample rate of 44.1 kHz, puts the first aliased component at at least 22.05 kHz. This is above the range of human hearing, so it works well.