Can someone please show me the code for 3.2? I have included code that may be needed for it. 3.2 is the second image; t
Posted: Mon May 09, 2022 7:25 am
Can someone please show me the code for 3.2? I have included code that may be needed for it.
3.2 is the second image; the previous image is there just in case.
Function for plot_freq_resp_magnitude (magnitude and gain mean the same thing here):
function plot_freq_resp_gain(f,H)
gainValue = 20*log10(abs(H));
plot(f, gainValue);
xlabel('Normal Freq in Hz');
ylabel('Gain in dB');
ylim([-100 10]);
grid on
end
Function for convolve:
function y = convolve(h,x)
N=length(x);
M=length(h);
m=M+N-1;
y=zeros(1,m);
xn=[x zeros(1,m-N)];
hn=[h zeros(1,m-M)];
for i=1:m
for j=1:i
y(i)=y(i)+xn(j)*hn(i-j+1);
end
end
disp(y)
end
Function for average power:
function pwr = avg_pwr(x)
siz = length(x)
sum = 0
for i = 1:siz
sum = sum + abs(x(i)*x(i));
end
pwr = sum/siz;
end
Function for 3.1:
clc
close all
M = 256;
L = 256;
n = 0:M*L;
max_freq = 0.5;
x = chirp(n, 0, M*L, max_freq);
spectrogram(x, L, 0, L, 1, 'yaxis');
h = ones(1,10)*0.1;
% change this function to convolve function u have written.
y = conv(x,h);
figure
spectrogram(y, L, 0, L, 1, 'yaxis');
f = zeros(1, 256);
H = zeros(1, 256);
for n=(1:M)
f(n) = f_i(n*L-L/2);
start_s = 1 + (n-1)*L;
end_s = n*L;
P = avg_pwr(y(start_s:end_s));
H(n) = sqrt(2*P);
end
figure
plot(f,H);
title('Power Spectral Density');xlabel('Frequency(Hz)');ylabel('Average Power');
function power = avg_pwr(x)
power = sqrt(sum(x.^2))/length(x);
end
function freq = f_i(t)
freq = (0.5/256) *floor(t/256);
end
3 Measuring Frequency response with a Chirp The frequency of a chirp signal increases with time. This suggests that we can measure the frequency response of a LTI system from the output of the system in response to a chirp signal. More specifically, we will measure the power of short segments of the output signal. 3.1 Assignment: Frequency Response using a Chirp Signal To fix ideas, we will first measure the frequency response by passing a chirp signal through a 10-point averager. We will then compute the average power in short segments of the output signal y[n]. Specifically, we will employ M-256 segments each of length L-256 samples. Write MATLAB code to accomplish the following: 1. Construct a chirp signal [nsuch that the instantaneous (discrete) frequency increases from 0 to over a period of ML = 65536 samples. Verify that you constructed the chirp correctly by plotting the spectrogram of 1 [n] (using the command spectrogram(x, L, 0, 1, 1, 'yaxis')). 2. Set the impulse response h[n] to that of a 10-point averager 3. Pass 2 [n] through the 10-point averager using your convolve function from part 2 and obtain the output signal y[n]. 4. Plot the spectrogram of y[n]. Explain how the effect of the filter manifests itself in the spectrogram. To extract the magnitude of the frequency response from the output signal y[n], we divide the output signal into M non-overlapping blocks of L samples. For each of these blocks, we compute the instantaneous frequency fa at the center of the block and compute the average power of the block of samples. The magnitude of the frequency response is then approximately equal to 1 (c)20/4) V2P. The following MATLAB code implements the above ideas. initialize storage f - zeros (1, 256); H - zeros (1, 256); for n- (1:M) n) - f_in*L-1/2); &form a block of samples start_s - 1 + (n-1) L; end_s - n-L; & compute average power and magnitude P - avg_pwr (y (start_s:end_s)); H(n) - sqrt(2*P); end Use your function plot_freq_resp_magnitude from part 1 to plot the magnitude of the frequency response and compare to the computed frequency response from part 1. The difference between the two should be small.
3.2 Assignment: Measure Complete Frequency Response Write a function that uses a chirp to measure the frequency response over a grid of M evenly spaced frequencies fd. Your function should contain code to . Construct a chirp of length M L samples, • Pass the chirp signal to a system that is passed as an input to your function, • Extract the magnitude of the frequency response from the output of the filter. Your function should be structured like this: function [H, f] - measure_freq_resp_chirp (sys_f_handle, M, L) % measure_freq_resp_chirp - measure the frequency response using a chirp H - measure_freq_resp_chirp (sys_f_handle 1, M] L]) g g g $ g g g g Input Variables: sys_f_hanfle - function handle to LTI system, will be invoked as y - sys_f_handle(x) M - number of blocks for computing average power L - number of samples per block g Output Variable: H - complex vector of length M; measured frequency response f - real vector of length M; frequency grid $check inputs if !isa (sys_f_handle, 'function_handle') error('measure_freq_resp: first input must be a function handle') end if nargin < 2 11 isempty (M) M - 512; end if nargin <3 || isempty (L) L-256; end %% Construct a chirp your code goes here 8% filter the chirp your code goes here %% Extract magnitude of frequency response $ your code goes here
3.2 is the second image; the previous image is there just in case.
Function for plot_freq_resp_magnitude (magnitude and gain mean the same thing here):
function plot_freq_resp_gain(f,H)
gainValue = 20*log10(abs(H));
plot(f, gainValue);
xlabel('Normal Freq in Hz');
ylabel('Gain in dB');
ylim([-100 10]);
grid on
end
Function for convolve:
function y = convolve(h,x)
N=length(x);
M=length(h);
m=M+N-1;
y=zeros(1,m);
xn=[x zeros(1,m-N)];
hn=[h zeros(1,m-M)];
for i=1:m
for j=1:i
y(i)=y(i)+xn(j)*hn(i-j+1);
end
end
disp(y)
end
Function for average power:
function pwr = avg_pwr(x)
siz = length(x)
sum = 0
for i = 1:siz
sum = sum + abs(x(i)*x(i));
end
pwr = sum/siz;
end
Function for 3.1:
clc
close all
M = 256;
L = 256;
n = 0:M*L;
max_freq = 0.5;
x = chirp(n, 0, M*L, max_freq);
spectrogram(x, L, 0, L, 1, 'yaxis');
h = ones(1,10)*0.1;
% change this function to convolve function u have written.
y = conv(x,h);
figure
spectrogram(y, L, 0, L, 1, 'yaxis');
f = zeros(1, 256);
H = zeros(1, 256);
for n=(1:M)
f(n) = f_i(n*L-L/2);
start_s = 1 + (n-1)*L;
end_s = n*L;
P = avg_pwr(y(start_s:end_s));
H(n) = sqrt(2*P);
end
figure
plot(f,H);
title('Power Spectral Density');xlabel('Frequency(Hz)');ylabel('Average Power');
function power = avg_pwr(x)
power = sqrt(sum(x.^2))/length(x);
end
function freq = f_i(t)
freq = (0.5/256) *floor(t/256);
end
3 Measuring Frequency response with a Chirp The frequency of a chirp signal increases with time. This suggests that we can measure the frequency response of a LTI system from the output of the system in response to a chirp signal. More specifically, we will measure the power of short segments of the output signal. 3.1 Assignment: Frequency Response using a Chirp Signal To fix ideas, we will first measure the frequency response by passing a chirp signal through a 10-point averager. We will then compute the average power in short segments of the output signal y[n]. Specifically, we will employ M-256 segments each of length L-256 samples. Write MATLAB code to accomplish the following: 1. Construct a chirp signal [nsuch that the instantaneous (discrete) frequency increases from 0 to over a period of ML = 65536 samples. Verify that you constructed the chirp correctly by plotting the spectrogram of 1 [n] (using the command spectrogram(x, L, 0, 1, 1, 'yaxis')). 2. Set the impulse response h[n] to that of a 10-point averager 3. Pass 2 [n] through the 10-point averager using your convolve function from part 2 and obtain the output signal y[n]. 4. Plot the spectrogram of y[n]. Explain how the effect of the filter manifests itself in the spectrogram. To extract the magnitude of the frequency response from the output signal y[n], we divide the output signal into M non-overlapping blocks of L samples. For each of these blocks, we compute the instantaneous frequency fa at the center of the block and compute the average power of the block of samples. The magnitude of the frequency response is then approximately equal to 1 (c)20/4) V2P. The following MATLAB code implements the above ideas. initialize storage f - zeros (1, 256); H - zeros (1, 256); for n- (1:M) n) - f_in*L-1/2); &form a block of samples start_s - 1 + (n-1) L; end_s - n-L; & compute average power and magnitude P - avg_pwr (y (start_s:end_s)); H(n) - sqrt(2*P); end Use your function plot_freq_resp_magnitude from part 1 to plot the magnitude of the frequency response and compare to the computed frequency response from part 1. The difference between the two should be small.
3.2 Assignment: Measure Complete Frequency Response Write a function that uses a chirp to measure the frequency response over a grid of M evenly spaced frequencies fd. Your function should contain code to . Construct a chirp of length M L samples, • Pass the chirp signal to a system that is passed as an input to your function, • Extract the magnitude of the frequency response from the output of the filter. Your function should be structured like this: function [H, f] - measure_freq_resp_chirp (sys_f_handle, M, L) % measure_freq_resp_chirp - measure the frequency response using a chirp H - measure_freq_resp_chirp (sys_f_handle 1, M] L]) g g g $ g g g g Input Variables: sys_f_hanfle - function handle to LTI system, will be invoked as y - sys_f_handle(x) M - number of blocks for computing average power L - number of samples per block g Output Variable: H - complex vector of length M; measured frequency response f - real vector of length M; frequency grid $check inputs if !isa (sys_f_handle, 'function_handle') error('measure_freq_resp: first input must be a function handle') end if nargin < 2 11 isempty (M) M - 512; end if nargin <3 || isempty (L) L-256; end %% Construct a chirp your code goes here 8% filter the chirp your code goes here %% Extract magnitude of frequency response $ your code goes here