Lab 6
Lab 6
Task 1.1: Find the impulse response, h[n], of the following lowpass filter
𝑯(𝝎) = { 𝟏 , |𝝎| < 𝝎𝟎
𝟎 , 𝝎𝟎 < |𝝎| < 𝝅} where ω0 = π/4. (Hint: h[n] is a sinc function)
N=(-25:1:25);
n = length(N);
for k=1:n
if N(k) ~= 0
h_n(k) = sin(N(k)*pi/4)./(N(k)*pi);
else
h_n(k) = 1/4;
end
end
plot(N,h_n);
Task 1.2: h[n] has infinite number of terms. In order to implement the digital filter, we will need to
limit the number of terms used. Write a MATLAB code to generate the 51 terms of h[n] for n= −25,
. . . , 25 and the store the 51 terms in a text file (an example is given in Sample File 1). Print out a stem
plot of the truncated impulse response h[n].
filename = ('N_values.txt');
fid = fopen(filename,"w");
fprintf(fid,'%d\r',h_n);
fclose(fid);
for k = 1:n
for j = 1:n-1
X(k,j) = h_n(j).*exp(-2*i*pi*N(j)*k./n);
end
end
Task 1.3: Plot the magnitude and phase responses of H(ω) vs. ω. (Hint: Fourier
transform of a real and even signal is a real and even function). Change the number of taps and
comment on the change in the magnitude response of H(ω).
w=linspace(0,2*pi,n);
X_2 = sum(X');
Phase = angle(X_2);
subplot(2,1,1)
plot(w,X_2)
title("Magnitude Response")
xlabel("\omega")
ylabel("Magnitude (V)")
subplot(2,1,2)
plot(w,Phase)
title("Phase Response")
xlabel("\omega")
ylabel("Angle (radians)")
Task 1.4: Up to this point you have used a rectangular window to limit the number of
terms of h[n]. Now, instead of using a rectangular window use a Hamming window. Print out a
stem plot of the new coefficients h[n]. Store the coefficients in another text file that is different from
Task 1.2. Plot the magnitude and phase responses of H(ω)
N=(-25:1:25);
n = length(N);
for k=1:n
if N(k) ~= 0
ham_n(k) = (sin(N(k)*pi/4)./(N(k)*pi)).*((25/46)+(21/46).*cos(2*pi*N(k)/n));
else
ham_n(k) = 1/4*((25/46)+(21/46));
end
end
figure(1)
plot(N,ham_n);
filename = ('N_values_Ham.txt');
fid = fopen(filename,"w");
fprintf(fid,'%d\r',ham_n);
fclose(fid);
for k = 1:n
for j = 1:n-1
X_ham(k,j) = ham_n(j).*exp(-2*i*pi*N(j)*k./n);
end
end
w=linspace(0,2*pi,n);
X_ham2 = sum(X_ham');
Ham_Phase = angle(X_ham2);
figure(2)
subplot(2,1,1)
plot(w,X_ham2,'m');
text(pi/5,1/sqrt(2),"\bullet Cutoff frequency is where \omega ~ \pi/5")
text(pi/4,1/13,"\bullet Frequency is where \omega = \pi/4")
title("Ham Magnitude Response")
xlabel("\omega")
ylabel("Magnitude (V)")
subplot(2,1,2)
plot(w,Ham_Phase)
title("Ham Phase Response")
xlabel("\omega")
ylabel("Angle (radians)")
Task 2.1: Write a code to compute the following equation
𝒚[𝒏] = ∑ 𝒉[𝒌]𝒙[𝒏 − 𝒌]
𝑵
𝒏=𝟎
where h[k] are the coefficients that you have generated in Part 1 for the lowpass filter using a
rectangular window and x[n] are the data read from the white channel of the codec. In order to
save memory, create a circular buffer of length N to store x[n]. Output the original signal x[n]from
the white channel on the white channel without change and output y[n] on the red channel. Connect
a function generator to the board input and an oscilloscope to the output. Notice the two channels on
the oscilloscope as you are varying the frequency on the function generator. Measure the magnitude
response (NO phase response) of the filter and compare it with the MATLAB results obtained in
Task 1.3.
#define N 51
volatile int number = 0;
volatile float h_n[] = {9.003163e-03, -9.745430e-18,-9.786047e-03,-1.446863e-02,-1.071805e-
02,9.745430e-18,1.184627e-02,1.768388e-02, 1.323995e-02,-9.745430e-18,-1.500527e-02,-2.273642e-
02,-1.731378e-02,9.745430e-18,2.046173e-02,3.183099e-02,2.500879e-02, -9.745430e-18,-3.215415e-
02,-5.305165e-02,-4.501582e-02,9.745430e-18,7.502636e-02,1.591549e-01,2.250791e-01,2.500000e-01,
2.250791e-01,1.591549e-01,7.502636e-02,9.745430e-18,-4.501582e-02,-5.305165e-02,-3.215415e-02,-
9.745430e-18,2.500879e-02,
3.183099e-02,2.046173e-02,9.745430e-18,-1.731378e-02,-2.273642e-02,-1.500527e-02,-9.745430e-
18,1.323995e-02,1.768388e-02, 1.184627e-02,9.745430e-18,-1.071805e-02,-1.446863e-02,-9.786047e-
03,-9.745430e-18,9.003163e-03};
int16_t buffer[N];
volatile int n = 0;
void PUT_YOUR_LAB_CODE_HERE(int16_t input_left, int16_t input_right, int16_t& output_left,
int16_t&
output_right) {
output_left = input_left;
buffer[n] = input_left;
float sum = 0;
for (int k = 0; k < N; k++){
sum = sum + buffer[(n+k) % N]*h_n[k];
}
output_right = sum;
n = (n+1) % N;
}
/* USER CODE END
Task 2.2: Now use the coefficients generated using the Hamming window. Measure the
magnitude response (NO phase response) of the filter and compare it to the MATLAB results. If we
increase the number of non-zero terms in h[n], what are the gain and the cost?
#define N 51
volatile int number = 0;
volatile float ham_n[] = {7.906793e-04, -9.231813e-19, -1.061194e-03, -1.862751e-03, -1.664896e-03,
1.830299e-18, 2.673442e-03, 4.745690e-03,4.173461e-03, -3.562272e-18, -6.280384e-03, -1.076460e-02,
-9.166253e-03, 5.706931e-18, 1.311891e-02, 2.212908e-02, 1.868075e-02,-7.753901e-18, -2.702556e-02,
-4.673064e-02, -4.123853e-02, 9.216053e-18, 7.271345e-02, 1.569605e-01, 2.243003e-01, 2.500000e-
01,2.243003e-01, 1.569605e-01, 7.271345e-02, 9.216053e-18, -4.123853e-02, -4.673064e-02, -
2.702556e-02, -7.753901e-18, 1.868075e-02,2.212908e-02, 1.311891e-02, 5.706931e-18, -9.166253e-03,
-1.076460e-02, -
6.280384e-03, -3.562272e-18, 4.173461e-03, 4.745690e-03,2.673442e-03, 1.830299e-18, -1.664896e-03,
-1.862751e-03, -1.061194e-03, -9.231813e-19, 7.906793e-04};
int16_t buffer[N];
volatile int n = 0;
void PUT_YOUR_LAB_CODE_HERE(int16_t input_left, int16_t input_right, int16_t& output_left,
int16_t&
output_right) {
output_left = input_left;
buffer[n] = input_left;
float sum = 0;
for (int k = 0; k < N; k++){
sum = sum + buffer[(n+k) % N]*ham_n[k];
}
output_right = sum;
n = (n+1) % N;
}
/* USER CODE END 0 */