audioplayer function: clicks during playback of sine waves

3 views (last 30 days)
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.

Accepted Answer

Daniel Shub
Daniel Shub on 15 Jun 2012
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.
  1 Comment
Walter Roberson
Walter Roberson on 16 Jun 2012
I know I didn't follow that, but in the part that I did follow, I don't see how that would add up to more than 4 times the sampling frequency ?
If you have a sine wave of frequency F, then what output frequency rate of a "sample and hold system" do you need to avoid aliasing?

Sign in to comment.

More Answers (1)

Daniel Shub
Daniel Shub on 15 Jun 2012
That is because MATLAB sound support is essentially broken. Look for some of the answer on the site referring to port audio.
Also note that with a 2KHz sample rate you are going to get aliasing.
  1 Comment
Walter Roberson
Walter Roberson on 15 Jun 2012
Why would there be aliasing when the sample rate is at least 4 times the frequency? I would expect aliasing if the sample rate was not at least twice the frequency?

Sign in to comment.

Categories

Find more on Measurements and Spatial Audio 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!