Problem 2.1:: Frequency (HZ) Amplitude Spectrum (DFT)
Problem 2.1:: Frequency (HZ) Amplitude Spectrum (DFT)
1:
close all;
clear all;
subplot(2,1,2);
plot(f, P);
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (DFT)');
figure;
subplot(2,1,1);
plot(f, xf(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Amplitude spectrum (DFT)');
subplot(2,1,2);
plot(f, P(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (DFT)');
% Zero padding to the length of 1024
x = [x, zeros(1, 24)];
N = length(x);
xf = abs(fft(x)) / N; % Compute the amplitude spectrum with zero padding
P = xf .* xf; % Compute the power spectrum
f = (0:N-1) * fs / N; % Map frequency bin to frequency (Hz)
figure;
subplot(2,1,1);
plot(f, xf);
grid on;
xlabel('Frequency (Hz)');
ylabel('Amplitude spectrum (FFT)');
subplot(2,1,2);
plot(f, P);
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (FFT)');
figure;
subplot(2,1,1);
plot(f, xf(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Amplitude spectrum (FFT)');
subplot(2,1,2);
plot(f, P(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (FFT)');
Problem 2.3:
% Define parameters
NoBits = 3; % Number of bits used in quantization
Xmin = -5; % Minimum value of the input signal range
Xmax = 5; % Maximum value of the input signal range
value = 2.7; % Input value to be quantized
% Display results
disp(['Input value: ', num2str(value)]);
disp(['Quantization index (I): ', num2str(I)]);
disp(['Quantized value (pq): ', num2str(pq)]);
Problem 4.1:
% Example 4.8
close all;
clear all;
subplot(2,1,2);
plot(f, P);
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (DFT)');
figure;
subplot(2,1,1);
plot(f, xf(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Amplitude spectrum (DFT)');
subplot(2,1,2);
plot(f, P(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (DFT)');
figure;
subplot(2,1,1);
plot(f, xf);
grid on;
xlabel('Frequency (Hz)');
ylabel('Amplitude spectrum (FFT)');
subplot(2,1,2);
plot(f, P);
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (FFT)');
figure;
subplot(2,1,1);
plot(f, xf(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Amplitude spectrum (FFT)');
subplot(2,1,2);
plot(f, P(1:N/2 + 1));
grid on;
xlabel('Frequency (Hz)');
ylabel('Power spectrum (FFT)');
Problem 6.1:
for i = 1:20
y(i + 2) = 2 * x(i + 2) - 4 * x(i + 1) - 0.5 * y(i + 1) + 0.5 * y(i);
% Compute 20 outputs
end
subplot(3,1,2);
stem(n, y(3:22));
grid on;
xlabel('Number of samples, n (Nonzero initial conditions)');
ylabel('Output y(n)');
disp('Output y(n) with nonzero initial conditions:');
disp(y(3:22));
Problem 6.2:
% Example 6.12
% Case b
figure(2)
h = freqz([1, -0.5, 1], 1, 1024); % Calculate the frequency response
phi = 180 * unwrap(angle(h)) / pi;
subplot(2, 1, 1), plot(w, abs(h)), grid on, xlabel('Frequency (radians)'),
ylabel('Magnitude')
subplot(2, 1, 2), plot(w, phi), grid on, xlabel('Frequency (radians)'),
ylabel('Phase (degrees)')
title('Case b')
% Case c
figure(3)
h = freqz([0.5, -0.32], [1, -0.5, 0.25], 1024); % Calculate the frequency
response
phi = 180 * unwrap(angle(h)) / pi;
subplot(2, 1, 1), plot(w, abs(h)), grid on, xlabel('Frequency (radians)'),
ylabel('Magnitude')
subplot(2, 1, 2), plot(w, phi), grid on, xlabel('Frequency (radians)'),
ylabel('Phase (degrees)')
title('Case c')
% Case d
figure(4)
h = freqz([1, -0.9, 0.81], [1, -0.6, 0.36], 1024); % Calculate the
frequency response
phi = 180 * unwrap(angle(h)) / pi;
subplot(2, 1, 1), plot(w, abs(h)), grid on, xlabel('Frequency (radians)'),
ylabel('Magnitude')
subplot(2, 1, 2), plot(w, phi), grid on, xlabel('Frequency (radians)'),
ylabel('Phase (degrees)')
title('Case d')
Problem 6.3:
close all;
clear all;
fs = 8000; % Sampling rate
alpha = 0:9; % Degree of pre-emphasis
figure(1);
freqz([1-alpha],1,512,fs); % Calculate and display frequency responses
load speech.dat
figure(2);
y = filter([1-alpha],1,speech); % Filtering speech
subplot(2,1,1), plot(speech, 'k'), grid on;
ylabel("Speech samples");
title("Speech: We lost the golden chain.")
subplot(2,1,2), plot(y, 'k'), grid on;
ylabel("Filtered samples");
xlabel("Number of samples");
title("Pre-emphasized speech.");
figure(3);
N = length(speech); % Length of speech
Axk = abs(fft(speech.*hamming(N)'))/N; % Two-sided spectrum of speech
Ayk = abs(fft(y.*hamming(N)'))/N; % Two-sided spectrum of pre-emphasized
speech
f = [0:N/2]*fs/N;
Axk(2:N/2+1) = 2*Axk(2:N/2+1); % Get one-sided spectrum of speech
Ayk(2:N/2+1) = 2*Ayk(2:N/2+1); % Get one-sided spectrum of filtered speech
subplot(2,1,1), plot(f, Axk(1:N/2+1), 'k'), grid on;
ylabel('Amplitude spectrum Ak')
title('Original speech');
subplot(2,1,2), plot(f, Ayk(1:N/2+1), 'k'), grid on;
ylabel('Amplitude spectrum Ak')
xlabel('Frequency (Hz)');
title('Preemphasized speech');
Problem 7.1:
N = 25;
fs = 8000;
% Design using the rectangular window
Ftype = 1; WnL = 0.2; WnH = 0; Wtype = 1;
Brec = fir1(N, WnL, 'low', rectwin(N+1));
% Design using the Hamming window
Ftype = 1; WnL = 0.2; WnH = 0; Wtype = 4;
Bham = fir1(N, WnL, 'low', hamming(N+1));
subplot(2,1,1);
plot(f, 20*log10(abs(hrec)), '-', f, 20*log10(abs(hham))); grid on;
axis([0 4000 -100 10]);
xlabel("Frequency (Hz)"); ylabel("Magnitude Response (dB)");
subplot(2,1,2);
plot(f, prec, '-', f, pham); grid on;
xlabel("Frequency (Hz)"); ylabel("Phase (degrees)");
Problem 7.5:
clc;
clf;
close all;
clear all;
% Filter specifications
Fs = 8000; % Sampling rate
Fstop1 = 500; % Lower stopband frequency (Hz)
Fpass1 = 1600; % Lower passband frequency (Hz)
Fpass2 = 2300; % Upper passband frequency (Hz)
Fstop2 = 3500; % Upper stopband frequency (Hz)
Astop = 50; % Stopband attenuation (dB)
Apass = 0.05; % Passband ripple (dB)
% Normalize frequencies
Fnorm = [Fstop1 Fpass1 Fpass2 Fstop2] / (Fs/2);
Problem 7.8:
% Define parameters
subplot(2,1,2);
plot(t, x_filtered);
title('Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
Problem 7.9:
clc;
clf;
close all;
clear all;
% Filter specifications
Fs = 8000; % Sampling rate
Fstop1 = 500; % Lower stopband frequency (Hz)
Fpass1 = 1600; % Lower passband frequency (Hz)
Fpass2 = 2300; % Upper passband frequency (Hz)
Fstop2 = 3500; % Upper stopband frequency (Hz)
Astop = 50; % Stopband attenuation (dB)
Apass = 0.05; % Passband ripple (dB)
% Normalize frequencies
Fnorm = [Fstop1 Fpass1 Fpass2 Fstop2] / (Fs/2);
clc;
clf;
close all;
clear all;
catch ME
disp(ME.message);
end
Problem 7.11:
clc;
clf;
close all;
clear all;
catch ME
disp(ME.message);
end
Problem 7.12:
clc;
clf;
close all;
clear all;
KK = length(b);
% Display output
disp('Filtered Output:');
disp(out);
% Plot output
figure;
subplot(2, 1, 1);
stem(sample, out, 'filled');
xlabel('Sample');
ylabel('Output');
title('Filtered Output');
grid on;
subplot(2, 1, 2);
stem(b, 'filled');
xlabel('Coefficient Index');
ylabel('Coefficient Value');
title('FIR Filter Coefficients');
grid on;
Problem 7.13:
clc;
clf;
close all;
clear all;
% Define filter specifications
Fs = 8000; % Sampling frequency (Hz)
Fpass = 100; % Passband frequency (Hz)
Fstop = 150; % Stopband frequency (Hz)
Apass = 0.5; % Passband ripple (dB)
Astop = 40; % Stopband attenuation (dB)
Problem 7.14:
clc;
clf;
close all;
clear all;
f1=100;f2=200;%the frequencies of sines signal that needs filtered
fs=2000;%sampling frequency
m=(0.3*f1)/(fs/2);%define tansition bandwidth
M=round(8/m);%define the window length
N=M-1;%define the order of filter
b=fir1(N,0.5*f2/(fs/2));%use the firl function to design a filter
%Input parameters are respectively the order number and the cutoff
%frequency of filter
figure(1)
[h,f]=freqz(b,1,512);%amplitude-frequency characteristic graph
plot(f*fs/(2*pi),20*log10(abs(h)))%parameters are respectively frequency and
amplitude
xlabel('frequency/Hz');ylabel('gain/dB');title('The gain response of lowpass
filter');
figure(2)
subplot(211)
t=0:1/fs:0.2;%define the time domain and the steplength
s=sin(2*pi*f1*t)+sin(2*pi*f2*t);%signal before filtering
plot(t,s);%plot the signal graph before filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram before
filtering');
axis([0 0.1 -2 2]);
subplot(212)
Fs=fft(s,512);%transform the signal to frequency domain
AFs=abs(Fs);%take the amplitude
f=(0:255)*fs/512;%frequency sampling
plot(f,AFs(1:256));%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram before
filtering');
figure(3)
sf=filter(b,1,s);%use filter function to filter
subplot(211)
plot(t,sf)%plot the signal graph after filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram after
filtering');
axis([0.1 0.2 -2 2]);
subplot(212)
Fsf=fft(sf,512);%frequency-domain diagram after filtering
AFsf=abs(Fsf);%the amplitude
f=(0:255)*fs/512;%frequency sampling
plot(f,AFsf(1:256))%plot the frequency domain diagram after filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram
after filtering');
Problem 7.15:
clc;
clf;
close all;
clear all;
M=32;%the number of samples
Wp=0.6*pi;%passband cutoff frequency
m=0:M/2;%the sampling points
Wm=2*pi*m./(M+1);%stopband cutoff frequency
mtr=ceil(Wp*(M+1)/(2*pi));%round to positive part,i.e.ceil(3.5)=4;ceil(-
3.2)=-3;
Ad=[Wm>=Wp];
Ad(mtr)=0.28;
Hd=Ad.*exp(-j*0.5*M*Wm);%define frequency-domain sampling vector H(k))
Hd=[Hd conj(fliplr(Hd(2:M/2+1)))];
%fliplr is to realize the fliplr of matrix and conj is the conjugate
h=real(ifft(Hd));%h(n)=IDFT[H(k)]
w=linspace(0,pi,1000);%get 1000 row vectors between 0 and pi
H=freqz(h,[1],w);%the amplitude -frequency characteristic diagram of the
filter
figure(1)
plot(w/pi,20*log10(abs(H)));%parameters are respectively the normalized
frequency and amplitude
xlabel('the normailzed frequency');ylabel('gian/dB');title('The gain
response of highpass filter');
axis([0 1 -50 0]);
f1=200;f2=700;f3=800;%the frequencies of sines signal that needs filtered
fs=2000;%the sample frequency
figure(2)
subplot(211)
t=0:1/fs:0.25;%define the time domain and steplength
s=sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);%signal before filtering
plot(t,s);%plot the diagram before filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram before
filtering');
subplot(212)
Fs=fft(s,512);%transform to the frequency domain
AFs=abs(Fs);%the amplitude
f=(0:255)*fs/512;%frequency sampling
plot(f,AFs(1:256));%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram
before filtering');
figure(3)
sf=filter(h,1,s);%use function filter
subplot(2,1,1)
plot(t,sf)%plot the diagram after filtering
xlabel('time/s');ylabel('amplitude');title('Time-domain diagram after
filtering')
axis([0.2 0.25 -2 2]); %set the range of image coordinates
subplot(2,1,2)
Fsf=fft(sf,512);AFsf=abs(Fsf);
f=(0:255)*fs/512;%frequency sampling
plot(f,AFsf(1:256))%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');title('Frequency-domain diagram
after filtering');
Problem 8.1:
clc;
clf;
close all;
clear all;
syms s
Problem 8.3:
Problem 8.4:
% Design of the digital highpass Butterworth filter
format long;
fs = 8000; % Sampling rate
[B, A] = lp2hp([1.9652],[1, 1.9652], 3.8627*10^4); % Complete step 2
[b, a] = bilinear(B, A, fs); % Complete step 3
Problem 8.6:
format long;
fs = 8000; % Sampling rate
% Lowpass to Highpass Transformation
[B, A] = lp2hp([1.4314], [1, 1.4256, 1.5162], 3.8627 * 10 ^ 4);
% Bilinear Transformation
[b, a] = bilinear(B, A, fs);
Problem 8.7:
format long
fs = 8000;
[B,A] = lp2bp([1],[1 1],sqrt(5.7499* 10 ^ 8),4088) ; % Complete step 2
[b,a] = bilinear(B,A,fs) % Complete step 3
% Plot the magnitude and phase responses
b = [0.0730, -0.0730]; %numerator coefficients from MATLAB
a =[1, 0.7117, 0.8541]; %denominator coefficients from MATLAB
freqz(b, a,512,fs);
axis([0, fs/2, -40, 10]);
Problem 8.8:
format long
fs = 8000; % Sampling rate
[B, A] = lp2bs([1],[1 1],sqrt(5.7341*10^8),4149)% Complete step 2
[b, a] = bilinear(B,A,fs)% Complete step 3
% Plot the magnitude and phase responses
b = [0.9259 0.7078 0.9259]; %numerator coefficients from MATLAB
a = [1 0.7078 0.8518]; %denominator coefficients from MATLAB
freqz(b,a,512,fs);
axis([0 fs/2 -40 10])
Problem 8.9:
format long
fs = 8000;
[B, A] = lp2bp([2.8628],[1 2.8628],sqrt(5.7341*10^8),4016) % Complete
step 2
[b, a] = bilinear(B,A,fs) % Complete step 3
% Plot the magnitude and phase responses
b = [0.1815 0.0 -0.1815]; %numerator coefficients from MATLAB
a = [1 0.6264 0.6369]; %denominator coefficients from MATLAB
freqz(b,a,512,fs);
axis([0 fs/2 -40 10])
Problem 8.10:
close all;
clear all;
figure(2);
g0 = 10;
g1 = 10;
g2 = 0;
g3 = 0;
g4 = 0;
g5 = 10;
g6 = 10;
p0 = 0;
p1 = pi/14;
p2 = 2 * p1;
p3 = 3 * p1;
p4 = 4 * p1;
p5 = 5 * p1;
p6 = 6 * p1;
y = g0 .* y0 + g1 .* y1 + g2 .* y2 + g3 .* y3 + g4 .* y4 + g5 .* y5 + g6
.* y6 + x; % Equalizer output
N = length(x);
Axk = 2 * abs(fft(x)) / N;
Axk(1) = Axk(1) / 2; % One-sided amplitude spectrum of the input
f = [0:N/2] * fs / N;
subplot(2,1,1);
loglog(f, Axk(1:N/2+1));
title('Audio spectrum');
axis([10 100000 0.00001 100]);
grid on;
Ayk = 2 * abs(fft(y)) / N;
Ayk(1) = Ayk(1) / 2; % One-sided amplitude spectrum of the output
subplot(2,1,2);
loglog(f, Ayk(1:N/2+1));
xlabel('Frequency (Hz)');
title('Equalized audio spectrum');
axis([10 100000 0.00001 100]);
grid on;
Problem 8.12:
%Example 8.15.
%Plot the magnitude responses |H(s)| and |H(z)| for the Laplace transfer function
H(s)
f = 0:0.1:5; % Frequency range and sampling interval
w = 2*pi*f; % Frequency range in rad/sec
hs = freqs([2],[1 2],w); % Analog frequency response
phis = 180*angle(hs)/pi;
Problem 8.13:
%Example 8.16
% Plot the magnitude responses jH(s)j and jH(z)j
% For the Laplace transfer function H(s)
f =0:0.1:5;
T =0.1; % Initialize analog frequency range in Hz and sampling interval
w =2*pi*f;
% Convert the frequency range to radians/second
hs =freqs([1 0],[1 2 5],w);
% Calculateanalogfilterfrequencyresponses
phis =180*angle(hs)/pi;
% For the z-transfer function H(z)
% Calculate digital filter frequency responses
hz =freqz([0.1 -0.09766],[1 -1.7735 0.8187],length(w));
phiz =180*angle(hz)/pi;
% Plot magnitude and phase responses
subplot(2,1,1), plot(f,abs(hs),'x',f, abs(hz),'-'),grid;
xlabel('Frequency (Hz)');ylabel('Magnitude Responses')
subplot(2,1,2), plot(f,phis, 'x',f, phiz,'-');grid;
xlabel('Frequency (Hz)');ylabel('Phases (degrees)')
Problem 8.14:
clc;
[N, Wn] = cheb2ord([Wp1, Wp2], [Wp1 - 0.05, Wp2 + 0.05], ripple, 3);
[num, den] = cheby2(N, ripple, [Wp1, Wp2], 'bandpass');
disp('Transfer function:');
disp(['Numerator: ', num2str(num)]);
disp(['Denominator: ', num2str(den)]);
disp(' ');
Problem 8.15:
%8.15
clc;
center_frequency = 2500;
bandwidth = 200;
ripple = 1;
Fs = 8000;
disp('Transfer function:');
disp(['Numerator: ', num2str(num)]);
disp(['Denominator: ', num2str(den)]);
disp(' ');
Problem 8.16:
clc;
clc;
fs = 8000;
fc = 1500;
Rp = 0.5;
Wn = fc / (fs/2);
a1 = a(2:end);
b1 = b(2:end);
diffeq = [b(1) a1];
figure(1);
subplot(2,1,1);
plot(f, mag);
grid on;
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
title('Magnitude Frequency Response');
subplot(2,1,2);
plot(f, phase);
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
title('Phase Frequency Response');
Problem 8.18:
Problem 8.19:
close all;
clear all;
fs = 8000;
t = 0:1/fs:1; x = zeros(1, length(t));
x(1) = 1;
y852 = filter([0 sin(2*pi*852/fs)], [1 -2*cos(2*pi*852/fs) 1], x);
y1209 = filter([0 sin(2*pi*1209/fs)], [1 -2*cos(2*pi*1209/fs) 1], x);
y7 = y852 + y1209;
subplot(2,1,1);
plot(t(1:400), y7(1:400));
grid on;
ylabel('y(n) DTMF: number 7');
xlabel('time (second)');
Ak = 2 * abs(fft(y7)) / length(y7);
Ak(1) = Ak(1) / 2;
f = [0:1:(length(y7) - 1)/2] * fs / length(y7);
subplot(2,1,2);
plot(f, Ak(1:(length(y7) + 1)/2));
grid on;
ylabel('Spectrum for y7(n)');
xlabel('frequency (Hz)');
Problem 8.20:
close all;
clear all;
N = 205;
fs = 8000;
t = [0:1:N-1]/fs;
x = zeros(1, length(t));
x(1) = 1;
y697 = filter([0 sin(2*pi*697/fs)], [1 -2*cos(2*pi*697/fs) 1], x);
y770 = filter([0 sin(2*pi*770/fs)], [1 -2*cos(2*pi*770/fs) 1], x);
y852 = filter([0 sin(2*pi*852/fs)], [1 -2*cos(2*pi*852/fs) 1], x);
y941 = filter([0 sin(2*pi*941/fs)], [1 -2*cos(2*pi*941/fs) 1], x);
y1209 = filter([0 sin(2*pi*1209/fs)], [1 -2*cos(2*pi*1209/fs) 1], x);
y1336 = filter([0 sin(2*pi*1336/fs)], [1 -2*cos(2*pi*1336/fs) 1], x);
y1477 = filter([0 sin(2*pi*1477/fs)], [1 -2*cos(2*pi*1477/fs) 1], x);
key = input('Input one of the following keys: 1, 2, 3, 4, 5, 6, 7, 8, 9, *, 0, #
=> ', 's');
yDTMF = [];
switch key
case '1'
yDTMF = y697 + y1209;
case '2'
yDTMF = y697 + y1336;
case '3'
yDTMF = y697 + y1477;
case '4'
yDTMF = y770 + y1209;
case '5'
yDTMF = y770 + y1336;
case '6'
yDTMF = y770 + y1477;
case '7'
yDTMF = y852 + y1209;
case '8'
yDTMF = y852 + y1336;
case '9'
yDTMF = y852 + y1477;
case '*'
yDTMF = y941 + y1209;
case '0'
yDTMF = y941 + y1336;
case '#'
yDTMF = y941 + y1477;
otherwise
disp('Invalid input. Please input a valid DTMF key.');
end
if ~isempty(yDTMF)
subplot(2, 1, 1);
plot(t, yDTMF(1:length(t)));
title(['DTMF Signal for Key "', key, '"']);
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
Y = fft(yDTMF);
f = (0:length(Y)-1) * (fs/length(Y));
subplot(2, 1, 2);
plot(f, abs(Y));
title(['Frequency Spectrum of DTMF Signal for Key "', key, '"']);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
xlim([0 2000]);
grid on;
end