Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

ASSIGNMENT 1:

Q1 (i). WRITE MATLAB CODE TO CONSTRUCT RECTANGULAR FUNCTION BY ADDING


SINE AND COSINE FUNCTIONS. ALSO EXPLAIN THE EFFECT OF NUMBER OF SINE
AND COSINE FUNCTIONS.

Q1 (ii). EXPLAIN THE EFFECT OF HARMONICS IN MEAN SQUARE ERROR OF RECT


FUNCTION AND CONSTRUCTED WAVEFORM.

clear all;
clc
opengl hardware
max = 9;
fs = 1000;

scope = dsp.SpectrumAnalyzer;
scope.SampleRate = fs;
scope.SpectralAverages = 1;
scope.PlotAsTwoSidedSpectrum = false;
scope.RBWSource = 'Auto';

for i = 1:2:max
% dsp.SineWave(amp,freq,phase,Name,Value);
wave = dsp.SineWave(1/i, i*2*pi, 0, ...
'SamplesPerFrame', 5000, 'SampleRate', fs);
y = wave();
if i == 1
wavesum = y;
else
wavesum = wavesum + y;
end

figure(1);
plot(wavesum(1:500));
title(sprintf('Sum to %i', i));
ylabel('Amplitude');
xlabel('Time');

scope(wavesum());
pause(.5);
% waitforbuttonpress;
end
For max = 1 (k = 1)
Sum to 1
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 50 100 150 200 250 300 350 400 450 500
Time

For max = 3 (k = 3)

Sum to 3
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 50 100 150 200 250 300 350 400 450 500
Time
For max = 5 (k = 5)
Sum to 5
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 50 100 150 200 250 300 350 400 450 500
Time

For max = 7 (k = 7)
Sum to 7
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 50 100 150 200 250 300 350 400 450 500
Time
For max = 9 (k = 9)
Sum to 9
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 50 100 150 200 250 300 350 400 450 500
Time

Q1 (ii). EXPLAIN THE EFFECT OF HARMONICS IN MEAN SQUARE ERROR OF RECT


--FUNCTION AND CONSTRUCTED WAVEFORM.

It is evident from the above waveforms, that increasing the number of harmonics reduces the
mean square error
Q2. GENERATE THE DTFT PLOTS IN MATLAB IN THE GIVEN DIAGRAM.

clear all
clc
opengl hardware

n = 0:9*pi;
x = (0.9).^n;

k = 0:900;
w = (pi/100)*k;
X=x*(exp(1i*pi/100)).^(-n'*k);

magX = abs(X);
angX =angle(X);

subplot (2,1,1); plot (w/pi,magX);grid; axis([0,9,0,15])


xlabel('freauency in units of pi'); ylabel(' |X|')
title('Magnitude Part')

subplot (2,1,2); plot (w/pi,angX/pi); grid; axis([0,9,-1*pi,1*pi])


xlabel('frequency in units of pi');
ylabel('radians/pi');
title('Angle Part')

Magnitude Part
15

10
|X|

0
0 1 2 3 4 5 6 7 8 9
freauency in units of pi

Angle Part
3

1
radians/pi

-1

-2

-3
0 1 2 3 4 5 6 7 8 9
frequency in units of pi
Q3. SQUARE WAVE SEQUENCE DTFT

clear all
clc
opengl hardware

n=0:0.01:100;
x=square(2*pi*n);

x1=abs(fft(x));

subplot(2,1,1);
plot(n,x);
title('Input Signal');
subplot(2,1,2);
plot(x1);
title('FFT');

Input Signal
1

0.5

-0.5

-1
0 10 20 30 40 50 60 70 80 90 100

FFT
7000

6000 X 101
Y 6365
5000

4000

3000

2000

1000

0
0 2000 4000 6000 8000 10000 12000
FFT LIVE SCRIPT
Noisy Signal
Use Fourier transforms to find the frequency components of a signal buried in noise.

Specify the parameters of a signal with a sampling frequency of 1 kHz and a signal duration of 1.5 seconds.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector

Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz sinusoid of amplitude 1.
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);

Corrupt the signal with zero-mean white noise with a variance of 4.


X = S + 2*randn(size(t));

Plot the noisy signal in the time domain. It is difficult to identify the frequency components by looking at the signal X(t).
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')

Compute the Fourier transform of the signal.


Y = fft(X);

Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L.
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

Define the frequency domain f and plot the single-sided amplitude spectrum P1. The amplitudes are not exactly at 0.7 and 1, as
expected, because of the added noise. On average, longer signals produce better frequency approximations.
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Now, take the Fourier transform of the original, uncorrupted signal and retrieve the exact amplitudes, 0.7 and 1.0.
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Copyright 2012 The MathWorks, Inc.
FILTER DESIGN TOOL AND FILTER EQUATION MATLAB CODE

You might also like