clear

load train
soundsc(y,Fs)
pause(.4)
soundsc(y,Fs)

% pause

% original
N=length(y); 
t=(0:N-1)'/N; 
x=y+0.2*(rand(size(y))-0.5); 
figure;subplot(2,1,1),plot(t,x);axis([t(1) t(end) -1 1])
set(gca,'fontsize',20)
xlabel('Time t');ylabel('Signal x')

% soundsc(x,Fs)
% pause(.4)
% soundsc(x,Fs)

X=fft(x); freq=(0:N/2-1)'; 
subplot(2,1,2),stem(freq,abs(X(freq+1))*2/N);axis([0 N/2 0 .2])
set(gca,'fontsize',20)
xlabel('Frequency k');ylabel('Fourier transform')

pause

% filtered
Y=zeros(size(X));
Y([1000:2000 N-2000:N-1000])=1; 
Z=X.*Y; 
z=real(ifft(Z));

soundsc(z,Fs)
pause(.4)
soundsc(z,Fs)

figure;subplot(2,1,1),plot(t,z);axis([t(1) t(end) -1 1])
set(gca,'fontsize',20)
xlabel('Time t');ylabel('Filtered signal')
subplot(2,1,2),stem(freq,abs(Z(freq+1))*2/N);axis([0 N/2 0 .2])
set(gca,'fontsize',20)
xlabel('Frequency k');ylabel('Fourier transform')

pause

% thresholded
threshold=0.031; 
Y=(abs(X)*2/N>threshold); 
Z=X.*Y; 
z=real(ifft(Z));

soundsc(z,Fs)
pause(.4)
soundsc(z,Fs)

figure;subplot(2,1,1),plot(t,z);axis([t(1) t(end) -1 1])
set(gca,'fontsize',20)
xlabel('Time t');ylabel('Thresholded signal')
subplot(2,1,2),stem(freq,abs(Z(freq+1))*2/N);axis([0 N/2 0 .2])
set(gca,'fontsize',20)
xlabel('Frequency k');ylabel('Fourier transform')

pause

threshold=0.1; 
Y=(abs(X)*2/N>threshold); 
Z=X.*Y; 
z=real(ifft(Z));

soundsc(z,Fs)
pause(.4)
soundsc(z,Fs)

figure;subplot(2,1,1),plot(t,z);axis([t(1) t(end) -1 1])
set(gca,'fontsize',20)
xlabel('Time t');ylabel('Thresholded signal')
subplot(2,1,2),stem(freq,abs(Z(freq+1))*2/N);axis([0 N/2 0 .2])
set(gca,'fontsize',20)
xlabel('Frequency k');ylabel('Fourier transform')