% M15-aliasing.diary
% Example of aliasing in speech
% 2008-11-25

% 8 kHz sampling (4 kHz bandwith) is the minimum to preserve good 
% intelligibilty

% Original is sampled at 16 kHz
[d,sr] = wavread('mpgr1_sx419.wav');
soundsc(d,sr)
% downsample() does low-pass filtering to avoid aliases
d8 = resample(d,1,2);
soundsc(d8,8000)
% 8 kHz sampling sounds OK
d4 = resample(d8,1,2);
soundsc(d4,4000)
% 4 kHz sampling is distinctly muffled
d2 = resample(d4,1,2);
soundsc(d2,2000)
% 2 kHz sampling (only 1 kHz of bandwidth) is very muffled

% Compare with and without anti-aliasing
% Simple decimation without low-pass filter will cause aliasing
d8a = d(1:2:end);
% Compare to original
soundsc(d8a,8000)
soundsc(d8,8000)
% Actually doesn't sound bad, because aliasing mainly occurs in fricatives
% listen to the distortion only (difference from "correct"):
soundsc(d8a-d8, 8000);
% Spectrogram makes differences visible
% Top pane shows original full-band signal
subplot(311)
specgram(d,512,16000);
caxis([-80 0])
% Show proper downsampled (just lower half of spectrum)...
subplot(312)
specgram(d8,256,8000);
caxis([-80 0])
% .. and compare to aliases
subplot(313)
specgram(d8a,256,8000);
caxis([-80 0])
% Where there is significant energy above 4 kHz in full-band signal,
% we see extra aliased energy folded down into bottom (decimated) signal.
