In: Computer Science
clear all
close all
%
%*******Parameter Settings********
%*********************************
Fs=8192; %Sampling frequency (MATLAB's default)
Ts=1/Fs; %Sampling time or sampling period
Vx=1; %Chirp signal time duration in seconds
A=1; %Amplitude of chirp signal
a=2*pi*3000; %Angular chirp rate (natural chirp rate is 3000)
b=1000; %Initial natural frequency in Hertz
%
Nx=Vx*Fs; %Number of samples of the generated chirp signal
Nxseg=round(Nx/16); %Length of a segment(1/16) of the chirp signal
txaxis=0:Ts:Nx*Ts-Ts; %Time-sampled axis (vector of time values)
Psx=1; %Pointer (>0) to the beginning of the segment sample
txseg=(Psx-1)*Ts:Ts:(Psx+Nxseg-2)*Ts; %Time-sampled axis for signal segment
fxinc=Fs/Nx; %Spectral resolution of x(t)
fxaxis=-(Fs/2):fxinc:(Fs/2)-fxinc; %Frequency axis
fsxinc=Fs/Nxseg; %Segment spectral resolution
fsxaxis=-(Fs/2):fsxinc:(Fs/2)-fsxinc; %Seg. freq. axis
%
%********Signals Generation*******
%*********************************
x=A*cos((0.5*a)*(txaxis.*txaxis)+2*pi*b*txaxis);
xseg=x(Psx:Psx+Nxseg-1); %Segment (1/16) of chirp signal
%
%*******Filtering Operations*******
%*********************************
Nh=251; %Bandpass filter order
Nhm1=Nh-1; %Filter parameter for fir1
Wn=[2*2000/Fs 2*(3500/Fs)]; %Nomalized cutoff frequency vector
hB=fir1(Nhm1,Wn); %Impulse response funtion
y=conv(x,hB); %Filtering the chirp signal
%
Ny=length(y); %Order or length of filtered signal
Nyseg=round(Ny/16); %Length of filtered signal segment(1/16)
Psy=1; %Pointer (>0) to the first filtered signal segment sample
yseg=y(Psy:Psy+Nyseg-1); %Segment (1/16) of filtered signal
tyaxis=0:Ts:Ny*Ts-Ts; %Time axis of filtered signal
tyseg=(Psy-1)*Ts:Ts:(Psy+Nyseg-2)*Ts;%Time-sampled axis for segment
fyinc=Fs/Ny; %Spectral resolution of y(t)
fyaxis=-(Fs/2):fyinc:(Fs/2)-fyinc; %Frequency axis
fsyinc=Fs/Nyseg; %Segment spectral resolution
fsyaxis=-(Fs/2):fsyinc:(Fs/2)-fsyinc; %Seg. freq. axis
%
%********Spectra Generation*******
%*********************************
X=fft(x); %Spectrum of x
sX=fftshift(X); %Change to the principal region
asX=abs(sX); %Magnitude of spectrum of x(t)
Xseg=fft(xseg); %Spectrum of xseg
sXseg=fftshift(Xseg); %Change to the principal region
asXseg=abs(sXseg); %Magnitude of spectrum of xseg(t)
%
Y=fft(y); %Spectrum of y
sY=fftshift(Y); %Change to the principal region
asY=abs(sY); %Magnitude of spectrum of y(t)
Yseg=fft(yseg); %Spectrum of yseg
sYseg=fftshift(Yseg); %Change to the principal region
asYseg=abs(sYseg); %Magnitude of spectrum of yseg(t)
%**********Signals Plots**********
%*********************************
plot(txaxis,x) %Chirp time-sampled input signal
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Chirp Input Signal x(t)')
grid
%
figure
plot(txseg,xseg) %Chirp time-sampled signal segment
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Chirp Input Signal Segment xseg(t)')
grid
%
figure
plot(tyaxis,y) %Chirp time-sampled filtered output signal
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Filtered Chirp Output Signal y(t)')
grid
%
figure
plot(tyseg,yseg) %Chirp time-sampled signal segment
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Filtered Chirp Output Signal Segment yseg(t)')
grid
%
figure
plot(fxaxis,asX)
xlabel('Frequency in Hertz')
ylabel('Magnitude')
title('Spectrum of Time-Sampled Chirp Input Signal x(t)')
grid
%
figure
plot(fsxaxis,asXseg)
xlabel('Frequency in Hertz')
ylabel('Magnitude in dB')
title('Spectrum of Time-Sampled Chirp Input Signal Segment xseg(t)')
grid
%
figure
plot(fyaxis,asY)
xlabel('Frequency in Hertz')
ylabel('Magnitude')
title('Spectrum of Time-Sampled Filtered Chirp Output Signal y(t)')
grid
%
figure
plot(fsyaxis,asYseg)
xlabel('Frequency in Hertz')
ylabel('Magnitude in dB')
title('Spectrum of Filtered Chirp Output Signal Segment yseg(t)')
grid
%
%**********Signals Sounds*********
%*********************************
disp('Sound of Generated Input Chirp Signal')
sound(x,Fs)
pause(05)
disp('Sound of Filtered Output Chirp Signal')
sound(y,Fs)
%
1. From the code above change the filter from a band-pass filter to a band-stop. You should also change the name of the impulse response function of the filter from hB to hS.
I JUST NEED THE MATLAB CODE that shows the change of filter.
ANSWER:
CODE TEXT
clear all
close all
%
%*******Parameter Settings********
%*********************************
Fs=8192; %Sampling frequency (MATLAB's default)
Ts=1/Fs; %Sampling time or sampling period
Vx=1; %Chirp signal time duration in second
A=1; %Amplitude of chirp signal
a=2*pi*3000; %Angular chirp rate (natural chirp rate is 3000)
b=1000; %Initial natural frequency in Hertz
%
Nx=Vx*Fs; %Number of samples of the generated chirp signal
Nxseg=round(Nx/16); %Length of a segment(1/16) of the chirp signal
txaxis=0:Ts:Nx*Ts-Ts; %Time-sampled axis (vector of time values)
Psx=1; %Pointer (>0) to the beginning of the segment sample
txseg=(Psx-1)*Ts:Ts:(Psx+Nxseg-2)*Ts; %Time-sampled axis for signal segment
fxinc=Fs/Nx; %Spectral resolution of x(t)
fxaxis=-(Fs/2):fxinc:(Fs/2)-fxinc; %Frequency axis
fsxinc=Fs/Nxseg; %Segment spectral resolution
fsxaxis=-(Fs/2):fsxinc:(Fs/2)-fsxinc; %Seg. freq. axis
%
%********Signals Generation*******
%*********************************
x=A*cos((0.5*a)*(txaxis.*txaxis)+2*pi*b*txaxis);
xseg=x(Psx:Psx+Nxseg-1); %Segment (1/16) of chirp signal
%
%*******Filtering Operations*******
%*********************************
Nh=251; %Bandpass filter order
Nhm1=Nh-1; %Filter parameter for fir1
Wn=[2*2000/Fs 2*(3500/Fs)]; %Nomalized cutoff frequency vector
% using ftype -> 'stop' in fir1 function to generate Band-Stop Filter
hS=fir1(Nhm1,Wn,'stop'); %Impulse response funtion
y=conv(x,hS); %Filtering the chirp signal
%
Ny=length(y); %Order or length of filtered signal
Nyseg=round(Ny/16); %Length of filtered signal segment(1/16)
Psy=1; %Pointer (>0) to the first filtered signal segment sample
yseg=y(Psy:Psy+Nyseg-1); %Segment (1/16) of filtered signal
tyaxis=0:Ts:Ny*Ts-Ts; %Time axis of filtered signal
tyseg=(Psy-1)*Ts:Ts:(Psy+Nyseg-2)*Ts;%Time-sampled axis for segment
fyinc=Fs/Ny; %Spectral resolution of y(t)
fyaxis=-(Fs/2):fyinc:(Fs/2)-fyinc; %Frequency axis
fsyinc=Fs/Nyseg; %Segment spectral resolution
fsyaxis=-(Fs/2):fsyinc:(Fs/2)-fsyinc; %Seg. freq. axis
%
%********Spectra Generation*******
%*********************************
X=fft(x); %Spectrum of x
sX=fftshift(X); %Change to the principal region
asX=abs(sX); %Magnitude of spectrum of x(t)
Xseg=fft(xseg); %Spectrum of xseg
sXseg=fftshift(Xseg); %Change to the principal region
asXseg=abs(sXseg); %Magnitude of spectrum of xseg(t)
%
Y=fft(y); %Spectrum of y
sY=fftshift(Y); %Change to the principal region
asY=abs(sY); %Magnitude of spectrum of y(t)
Yseg=fft(yseg); %Spectrum of yseg
sYseg=fftshift(Yseg); %Change to the principal region
asYseg=abs(sYseg); %Magnitude of spectrum of yseg(t)
%**********Signals Plots**********
%*********************************
plot(txaxis,x) %Chirp time-sampled input signal
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Chirp Input Signal x(t)')
grid
%
figure
plot(txseg,xseg) %Chirp time-sampled signal segment
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Chirp Input Signal Segment xseg(t)')
grid
%
figure
plot(tyaxis,y) %Chirp time-sampled filtered output signal
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Filtered Chirp Output Signal y(t)')
grid
%
figure
plot(tyseg,yseg) %Chirp time-sampled signal segment
xlabel('Time in Seconds')
ylabel('Amplitude')
title('Time-Sampled Filtered Chirp Output Signal Segment yseg(t)')
grid
%
figure
plot(fxaxis,asX)
xlabel('Frequency in Hertz')
ylabel('Magnitude')
title('Spectrum of Time-Sampled Chirp Input Signal x(t)')
grid
%
figure
plot(fsxaxis,asXseg)
xlabel('Frequency in Hertz')
ylabel('Magnitude in dB')
title('Spectrum of Time-Sampled Chirp Input Signal Segment xseg(t)')
grid
%
figure
plot(fyaxis,asY)
xlabel('Frequency in Hertz')
ylabel('Magnitude')
title('Spectrum of Time-Sampled Filtered Chirp Output Signal y(t)')
grid
%
figure
plot(fsyaxis,asYseg)
xlabel('Frequency in Hertz')
ylabel('Magnitude in dB')
title('Spectrum of Filtered Chirp Output Signal Segment yseg(t)')
grid
%
%**********Signals Sounds*********
%*********************************
disp('Sound of Generated Input Chirp Signal')
sound(x,Fs)
pause(05)
disp('Sound of Filtered Output Chirp Signal')
sound(y,Fs)
%
CODE IMAGE (OF CHANGED PORTION)