% M01-intro-demo.diary
% Matlab examples from lecture 1
% 2007-09-04

% Time dilation example
[d,sr] = wavread('mpgr1_sx419.wav');
soundsc(d,sr)
% Slow down sampling rate
soundsc(d,sr*.7)
% Slow down with SOLAFS
dd = solafs(d',0.7,300,100);
soundsc(dd,sr)
% Speed up
du = solafs(d',1.5,300,100);
soundsc(du,sr)
% Look at the waveforms
plot(d)
% Zoom in on the first part
plot(d(1:5000))
plot(d(2000+[1:5000]))
% Compare with time-dilated version
subplot(211)
plot(d(2000+[1:5000]))
subplot(212)
plot(dd(2000+[1:5000]))
% Compare spectrograms
subplot(211)
specgram(d,256,sr);
subplot(212)
specgram(dd,256,sr);
% spectrograms with same duration - extend shorter
d(length(dd)) = 0;
subplot(211)
specgram(d,256,sr);
% fix autoscaling of color axis (to low dB floor)
caxis([-70 10]);  % -70 dB to 10 dB
% match in second specgram
subplot(212)
caxis([-70 10]);


% Visualizing complex sequences
x = 10*exp((-1/12 + j*pi/6)*[0:39]);
x
subplot(211)
plot(real(x))
stem(real(x))
subplot(212)
stem(imag(x))
% Plot as 3-D
subplot(111)
stemz(x)
axis equal
% Add projections for real and imag
stemz([0:39],x,1)
axis equal

% Aperiodic sinusoidal sequence
stem([0:40],sin([0:40]*2*pi/(22/3)))
% superimpose `smooth' sinusoid for comparison
hold on; plot([0:.1:40],sin([0:.1:40]*2*pi/(22/3)),'r'); hold off
grid

% Aliased sinusoids - cross at sampling instants
xx = 0:0.01:10;
plot(xx,sin(2*pi*xx*1.2),'r',xx,sin(2*pi*xx*0.2),'g');
% superimpose the discrete samples (same for both freqs)
hold on; stem(0:10,sin([0:10]*2*pi*0.2)); hold off
