%Demo done during Session 18 demonstrating
%efficacy of Hilbert Transform in "zero'ing"
%the spectral content of a real-valued DT
%signal in the interval -pi < omega < 0
clf
set(0,'defaultaxesfontsize',20);
N=256;
%passband edges for speech sampled at 12.5 KHz
omegap=2*0.3/12.5;
Wp2=2*6/12.5;
ML=100;
hbt=remez(ML,[omegap Wp2],[1 1],'Hilbert');
%plot magnitude and phase of FIR HT designed
%via the Remez-Exchange Algorithm
hbtfft=fftshift(fft(hbt,N));
subplot(211)
hbtmag=20*log10(abs(hbtfft));
hbtang=angle(hbtfft);
freqomega=linspace(-pi,pi-2*pi/N,N);
Mdel=ML/2;
hbtang=angle(exp(j*Mdel*freqomega).*hbtfft);
plot(freqomega,hbtmag,'LineWidth',4);
axis([-pi pi -20 20]);
title('Hilbert Transform Response'),...
xlabel('Omega'), ylabel('Magnitude'), grid
subplot(212)
plot(freqomega,hbtang,'LineWidth',4);
axis([-pi pi -pi pi]);
title('Phase Response')
pause
fp=fopen('0uf1s1t0.wav','r');
[data,lengthd]=fread(fp,[1,1024],'char');
[data,lengthd]=fread(fp,[1,inf],'short');
fclose(fp);
Fs=12500;
clf
plot(data)
end1=input('end1?')
end2=input('end2?')
del=end2-end1;
datar=data(end1:end2);
%Hilbert Transform the data by convolving
%with impulse response from remez algorithm
yh=conv(datar,hbt);
delta=zeros(size(hbt));
delta((ML+2)/2)=1;
yd=conv(datar,delta);
%create complex analytic signal
yp=yd+j*yh;
%Compute FFT of complex analytic signal
%to show that its DTFT is 0 for -pi < w < 0
Ns=16384;
ypfft=fftshift(fft(yp,Ns));
clf
ypmag=(abs(ypfft));
ypmax=max(ypmag);
freqomega=linspace(-pi,pi,Ns);
plot(freqomega,ypmag,'LineWidth',4);
axis([-pi pi 0 ypmax]);
title('DTFT of Analytic Signal')
pause
%Create complex-valued impulse response
%and show is a bandpass filter passing
%the passband -pi < omega < 0
hc=delta+j*hbt; 
Ns=256;
hcfft=fftshift(fft(hc,Ns));
clf
hcmag=(abs(hcfft));
hcmax=max(hcmag);
freqomega=linspace(-pi,pi,Ns);
plot(freqomega,hcmag,'LineWidth',4);
axis([-pi pi 0 2]);
ylabel('Magnitude')
title('Frequency Response of Complex Filter')
