Instructions
Requirements and Specifications
Source Code
P1 A
%----------------------------------------------------------
% P1a
%
% Make a 2 second digital audio signal that contains a pure
% cosine tone with analog frequency 440 Hz.
% - play the signal through the sound card
% - plot the centered DFT magnitude in dB against
% Hertzian analog freq, radian digital freq,
% and normalized digital freq.
% - Write the signal to a wave file, read it back in, and
% play it through the sound card again.
%
Fs = 44100; % sampling frequency in Hz
N = Fs * 2; % length of the 2 sec signal
n = 0:N-1; % discrete time variable
f_analog = 5e3; % analog frequency in Hz
w_dig = 2*pi*f_analog/Fs; % radian digital frequency
x = cos(w_dig * n); % the signal
% Normalize samples to the range [-1,1]
% Not really needed here b/c cos is already in this range,
% but done anyway to illustrate how you normalize.
x = x / max(abs(x));
sound(x,Fs,16); % play it through sound card
X = fftshift(fft(x)); % centered DFT
Xmag = abs(X); % centered DFT magnitude
XmagdB = 20*log10(Xmag); % convert to dB
% Plot the centered magnitude against analog frequency
w = -pi:2*pi/N:pi-2*pi/N; % dig rad freq vector
f = w * Fs /(2*pi); % analog freq vector
figure(1);
plot(f,XmagdB);
xlim([-20000 20000]);
title("Centered DFT Magnitude for 5 kHz Pure Tone");
xlabel("analog frequency, Hz");
ylabel("dB");
% Plot the centered magnitude against radian digital freq
figure(2);
plot(w,XmagdB);
xlim([-pi pi]);
title("Centered DFT Magnitude for 5 kHz Pure Tone");
xlabel("radian digital frequency \omega");
ylabel("dB");
% Plot against normalized digital frequency
figure(3);
plot(w/pi,XmagdB);
xlim([-1 1]);
title("Centered DFT Magnitude for 5 kHz Pure Tone");
xlabel("normalized digital frequency \omega/\pi");
ylabel("dB");
% wait 3 seconds in case sound card is still busy
pause(3);
audiowrite("A-5000.wav",x,Fs); % write to wave file
[x2,Fs] = audioread("A-5000.wav"); % read it back in
sound(x2,Fs,16); % play it again Sam!
P2 A
%----------------------------------------------------------
% P2a
%
% Make some digital audio signals and demonstrate filtering.
% All signals are 4 seconds in duration.
% - Make x1 a 250 Hz pure tone.
% - Play x1 through the sound card.
% - Make x2 a swept frequency chirp from 1 kHz to 3 kHz.
% - Play x2 through the sound card.
% - Make x3 = x1 + x2.
% - Play x3 through the sound card.
% - Apply a lowpass digital Butterworth filter to x3 to
% keep the pure tone and reject the chirp.
% - Play the filtered signal through the sound card.
% - Apply a highpass digital Butterworth filter to x3 to
% keep the chirp and reject the pure tone.
% - Play the filtered signal through the sound card.
%
Fs = 44100; % sampling frequency in Hz
N = Fs * 4; % length of the 4 sec signal
n = 0:N-1; % discrete time variable
% Make x1 a 250 Hz pure tone
f_analog = 1e3; % pure tone analog frequency
w_dig = 2*pi*f_analog/Fs; % radian digital frequency
x1 = cos(w_dig * n); % the pure tone
sound(x1,Fs,16); % play it through sound card
pause(5); % wait for sound card to clear
% Make x2 4-second cosine pure tone with analog frequency 3kHz
Fs = 44100; % sampling frequency in Hz
N = Fs * 4; % length of the 4 sec signal
n = 0:N-1; % discrete time variable
% Make x1 a 250 Hz pure tone
f_analog = 3e3; % pure tone analog frequency
w_dig = 2*pi*f_analog/Fs; % radian digital frequency
x2 = cos(w_dig * n); % the pure tone
sound(x2,Fs,16); % play it through sound card
pause(5); % wait for sound card to clear
% Add the two signals
x3 = x1 + x2;
x3 = x3 / max(abs(x3)); % normalize the range to [-1,1]
sound(x3,Fs,16); % play it through sound card
pause(5); % wait for sound card to clear
% Use a lowpass digital Butterworth filter to keep the 250 Hz
% pure tone and reject the chirp.
w_dig = 2*pi*1e3 /Fs;
w_start_dig = 2*pi*3e3/Fs;
Wp = w_dig/pi; % normalized passband edge freq
Ws = w_start_dig/pi; % normalized stopband edge freq
Rp = 1; % max passband ripple
Rs = 60; % min stopband attenuation
[Nf, Wn] = buttord(Wp,Ws,Rp,Rs); % design filter order
[num,den] = butter(Nf,Wn); % design the filter
h=fvtool(num,den); % show frequency response
figure(2);
freqz(num,den,1024); % plot frequency response
title("Lowpass Frequency Response");
y1 = filter(num,den,x3); % apply the filter
y1 = y1 / max(abs(y1)); % normalize filtered signal
sound(y1,Fs,16); % play it through sound card
QUESTION 3
clc, clear all, close all
%% Question 3
% Read noisyavg.wav
[y1, Fs] = audioread('noisysig.wav');
[y2, Fs] = audioread('noisesamp.wav');
% Read
% Play
% sound(y, Fs)
% Get length of signals
N1 = length(y1);
N2 = length(y2);
% DFT magnitude plots for both signals
Y1 = fftshift(fft(y1)); % centered DFT
Ymag1 = abs(Y1); % centered DFT magnitude
YmagdB1 = 20*log10(Ymag1); % convert to dB
% Plot the centered magnitude against analog frequency
w1 = -pi:2*pi/N1:pi-2*pi/N1; % dig rad freq vector
f1 = w1 * Fs /(2*pi); % analog freq vector
Y2 = fftshift(fft(y2)); % centered DFT
Ymag2 = abs(Y2); % centered DFT magnitude
YmagdB2 = 20*log10(Ymag2); % convert to dB
% Plot the centered magnitude against analog frequency
w2 = -pi:2*pi/N2:pi-2*pi/N2; % dig rad freq vector
f2 = w2 * Fs /(2*pi); % analog freq vector
figure
subplot(1,2,1)
plot(w1/pi,YmagdB1);
xlim([-1, 1]);
title("Centered DFT Magnitude for noisysig.wav");
xlabel("normalized digital frequency \omega/\pi");
ylabel("dB");
grid on
subplot(1,2,2)
plot(w2/pi,YmagdB2);
xlim([-1, 1]);
title("Centered DFT Magnitude for noisesamp.wav");
xlabel("normalized digital frequency \omega/\pi");
ylabel("dB");
grid on
% Lowpass filter
w_dig = 2*pi*2.5e3 /Fs;
w_start_dig = 2*pi*5e3/Fs;
Wp = w_dig/pi; % normalized passband edge freq
Ws = w_start_dig/pi; % normalized stopband edge freq
Rp = 1; % max passband ripple
Rs = 60; % min stopband attenuation
[Nf, Wn] = buttord(Wp,Ws,Rp,Rs); % design filter order
[num,den] = butter(Nf,Wn); % design the filter
figure
h=fvtool(num,den); % show frequency response
y3 = filter(num, den, y1);
Y3 = fftshift(fft(y3)); % centered DFT
Ymag3 = abs(Y3); % centered DFT magnitude
YmagdB3 = 20*log10(Ymag3); % convert to dB
% Plot the centered magnitude against analog frequency
figure(3)
plot(f1,YmagdB3);
xlim([-1, 1]);
title("Centered DFT Magnitude for filtered noisysig.wav");
xlabel("normalized digital frequency \omega/\pi");
ylabel("dB");
grid on
sound(y3, Fs)
audiowrite("filteredsig.wav",y3,Fs); % write to wave file