0% found this document useful (0 votes)
143 views5 pages

Solutions HW 5

The document provides solutions to homework problems from a BME 301 class. It includes MATLAB code to: 1) Generate signals with added noise at different SNR levels and plot the magnitude spectra; 2) Generate signals with different numbers of points and plot the magnitude spectra to show the effect of data length on detection; 3) Generate a square wave signal, take its FFT, and plot the magnitude and phase spectra.

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views5 pages

Solutions HW 5

The document provides solutions to homework problems from a BME 301 class. It includes MATLAB code to: 1) Generate signals with added noise at different SNR levels and plot the magnitude spectra; 2) Generate signals with different numbers of points and plot the magnitude spectra to show the effect of data length on detection; 3) Generate a square wave signal, take its FFT, and plot the magnitude and phase spectra.

Uploaded by

Abcd Efgh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BME 301 Fall 2016 Thursday Sept 22, 2016

Homework #5 Solutions

Due: Thursday September 29, 2016 at the beginning of class

Problem 10 Page 127

% Prob 10 Use the routine sig_noise to generate a waveform containing a 200 and 400 Hz sine
% waves as in Problem 9, but add noise so that the Signal-to-Noise Ratio (SNR) is -8 dB; i.e.,
% x = sig_noise([200 400], -8,N) where N = 512.
% Plot the magnitude spectrum, but only plot the non-redundant points (1 to N/2).
% Repeat for an SNR of -16 dB. Note that the two sinusoids are hard to distinguish at the higher (-16 dB) noise level.
%
close all; clear all;
N = 512;
fs = 1000; % Assumed by sig_noise
f = (0:N-1)*fs/(N-1); % Freqeuency vector for plotting
x = sig_noise([200 400],-8,N); % Generate waveform
X = abs(fft(x)); % Calculate magnitude spectrum
plot(f(1:N/2),X(1:N/2)); % Plot only non-redundant points
xlabel('Freqeuncy Hz'); ylabel('|X(f)|');
title('SNR = -8 dB');
figure; % Repeat for SNR of -16 dB
x = sig_noise([200 400],-16,N);
X = abs(fft(x));
plot(f(1:N/2),X(1:N/2));
xlabel('Freqeuncy Hz'); ylabel('|X(f)|');
title('SNR = -16 dB');

SNR = -8 dB
140 SNR = -16 dB
60

120
50

100
40

80
|X(f)|

|X(f)|

30
60

20
40

10
20

0 0
0 50 100 150 200 250 300 350 400 450 500 0 50 100 150 200 250 300 350 400 450 500
Freqeuncy Hz Freqeuncy Hz
Problem 11

% Prob 11 Use the routine sig_noise to generate a waveform containing 200 and 400 Hz sine waves
% as in Problem 9 and 10 with an SNR of -12 dB with 1000 points. Plot the non-redundant
% points in the magnitude spectrum. Repeat for the same SNR, but for a signal with only 100 points.
% Note that the two sinusoids are hard to distinguish with the smaller data sample.
%
close all; clear all;
N = 512;
fs = 1000; % Assumed by sig_noise
f = (0:N-1)*fs/(N-1); % Frequency vector for plotting
x = sig_noise([200 400],-12,N); % Generate waveform
X = abs(fft(x)); % Calculate magnitude spectrum
plot(f(1:N/2),X(1:N/2)); % Plot only non-redundant points
xlabel('Freqeuncy Hz'); ylabel('|X(f)|');
title('SNR = -12 dB, N = 512');
figure; % Repeat for N = 200
N = 100;
f = (0:N-1)*fs/(N-1); % Freqeuency vector for plotting
x = sig_noise([200 400],-12,N);
X = abs(fft(x));
plot(f(1:N/2),X(1:N/2));
xlabel('Freqeuncy Hz'); ylabel('|X(f)|');
title('SNR = -12B, N = 100');
%Note that the two sinusoids are difficult to distinguish with the smaller
%data length. Thus, from problem 10 we see that noise level and in problem
%11 that data length are important when detecting sinusoids.

SNR = -12 dB, N = 512 SNR = -12B, N = 100


90 30

80
25
70

60
20

50
|X(f)|

|X(f)|

15
40

30
10

20

5
10

0
0 50 100 150 200 250 300 350 400 450 500 0
0 50 100 150 200 250 300 350 400 450 500
Freqeuncy Hz
Freqeuncy Hz
Problem 12 Page 127

% Prob 3.12 Generate the signal shown in Problem 2 on page 124 and use MATLAB to
% find both the magnitude and phase spectrum of this signal.
% Assume that the period, T, is 1 sec. and assume a sampling frequency
% of 500 Hz; hence you will need to use 500 points to generate the signal.
% Plot only the non-redundant points on both the magnitude and phase curve.
%
clear all; close all;
fs = 500; % Sampling frequency
N = 500; % Number of points for 1 sec of data
t = (1:N)/fs; % Time vector for ploting and signal generation
f = (1:N)*fs/N; % Frequency vector for plotting
x = 2* [t(1:N/2) -t(1:N/2)]; % Generate x(t)
plot(t,x); % Plot x(t)
xlabel('Time (sec)'); ylabel('x(t)');
X = fft(x); % Calculate complex FT
Mag = abs(X); % Get magnitude spectrum
Phase = angle(X); % Get phase spectrum
figure; % Plot spectra
subplot(1,2,1);
plot(f(1:20),Mag(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('|X(f)|');
subplot(1,2,2);
plot(f(1:20),Phase(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('Phase(f) (rad)');

1 200 0

0.8 180

0.6 160 -0.5

0.4 140

0.2 120 -1
Phase(f) (rad)
|X(f)|
x(t)

0 100

-0.2 80 -1.5

-0.4
60

-0.6
40 -2
-0.8
20

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 -2.5
0 5 10 15 20 0 5 10 15 20
Time (sec)
Frequency(Hz) Frequency(Hz)
Problem 13 Page 127
% 13) Repeat Problem 12 using the signal in Problem 4 on page 125,
% but generate the signal between –T/2 and +T/2 (i.e., x = t; for t = (-N/2:N/2); ).
% Also plot the phase in degrees; i.e., scale by 360/(2?).
%
clear all; close all;
fs = 500; % Sampling frequency
N = 500; % Number of points for 1 sec of data
t = (-N/2:N/2)/fs; % Time vector for signal generation and plotting
f = (1:N)*fs/N; % Frequency vector for plotting
x = t; % Generate x(t)
plot(t,x); % Plot x(t)
xlabel('Time (sec)'); ylabel('x(t)');
X = fft(x); % Calcualate FT
Mag = abs(X); % Get magnitude spectrum
Phase = angle(X)*360/(2*pi) % Get phase spectrum
figure; % Plot spectra
subplot(1,2,1);
plot(f(1:20),Mag(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('|X(f)|');
subplot(1,2,2);
plot(f(1:20),Phase(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('Phase(f) (rad)');

0.5
80 100

0.4
90
70
0.3
80
60
0.2
70
0.1 50
60

Phase(f) (rad)
x(t)

|X(f)|

0
40 50
-0.1
40
30
-0.2
30
20
-0.3
20
-0.4 10
10

-0.5
-0.5 -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.5 0 0
0 5 10 15 20 0 5 10 15 20
Time (sec)
Frequency(Hz) Frequency(Hz)
Problem 14 Page 127
%14) Repeat Problem 13 for the signal in Problem 4 on page 125,
% but generate the signal between 0 and T.
%
clear all; close all;
fs = 500; % Sampling frequency
N = 500; % Number of points for 1 sec of data
t = (1:N)/fs; % Time vector for signal generation and plotting
f = (1:N)*fs/N; % Frequency vector for plotting
x = 2*[t(1:N/2) t(1:N/2)-.5]; % Generate x(t)
plot(t,x);
xlabel('Time (sec)'); ylabel('x(t)');
X = fft(x); % Calculate FT
Mag = abs(X); % Get magnitude
Phase = angle(X)*360/(2*pi); % Get phasem convert to deg
figure; % Plot spectra
subplot(1,2,1);
plot(f(1:20),Mag(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('|X(f)|');
subplot(1,2,2);
plot(f(1:20),Phase(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('Phase(f) (rad)');
figure;
Phase = unwrap(angle(X))*360/(2*pi); % Unwarp phase and convert to deg.
subplot(1,2,1); % Plot new spectra
plot(f(1:20),Mag(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('|X(f)|');
subplot(1,2,2);
plot(f(1:20),Phase(1:20),'*');
xlabel('Frequency(Hz)'); ylabel('Phase(f) (rad)');
1 160 100

0.8 80
140
0.6 60
120
0.4 40
100
0.2 20
Phase(f) (rad)
|X(f)|
x(t)

0 80 0

-0.2 -20
60
-0.4 -40
40
-0.6 -60
20
-0.8 -80

-1 0 -100
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 5 10 15 20 0 5 10 15 20
Time (sec) Frequency(Hz) Frequency(Hz)

160 0

140 -500

120
-1000

100
Phase(f) (rad)

-1500
|X(f)|

80
-2000
60

-2500
40

20 -3000

0 -3500
0 5 10 15 20 0 5 10 15 20
Frequency(Hz) Frequency(Hz)

You might also like