0% found this document useful (0 votes)
28 views

Experiment 1 - DSP

The document describes an experiment conducted in MATLAB to: 1) Count the occurrences of a number in a matrix and plot discrete time signals like unit step, unit impulse and unit ramp. 2) Generate a 100Hz sine wave sampled at various rates and observe the effects of sampling. 3) Generate and plot the sum of three sinusoidal signals. 4) Add Gaussian noise to the generated signal and observe the effect of 20dB SNR noise.

Uploaded by

Rahul Gandhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Experiment 1 - DSP

The document describes an experiment conducted in MATLAB to: 1) Count the occurrences of a number in a matrix and plot discrete time signals like unit step, unit impulse and unit ramp. 2) Generate a 100Hz sine wave sampled at various rates and observe the effects of sampling. 3) Generate and plot the sum of three sinusoidal signals. 4) Add Gaussian noise to the generated signal and observe the effect of 20dB SNR noise.

Uploaded by

Rahul Gandhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment – 1

Aim: Introduction to MATLAB and generate various discrete time signals


Laboratory Exercise
a. For a given M ×M matrix denoted with variable X, count the occurrence of a given
number N.
MATLAB Code:
clc; close all; clear all;
x = input('Enter a matrix: ');
n = input('Enter a number that is present in the matrix: ')
x, n
[p,q]= size(x);
count = 0;
for i = 1:p
for j = 1:q
if x(i,j) == n
count = count + 1;
end
end
end
fprintf('The number of times element %d occured in given matrix
is %d\n',n,count)

Results:
b. Generate discrete unit step, unit impulse, unit ramp signals and plot the same.
MATLAB Code:
clc; close all; clear all;
n = (-10:10);
u = n>=0;
i = n==0;
r = n.*u;
subplot(311); stem(n,u);
xlabel('n'); ylabel('Amplitude'); title('Unit Step');
subplot(312); stem(n,i);
xlabel('n'); ylabel('Amplitude'); title('Unit Impulse');
subplot(313); stem(n,r);
xlabel('n'); ylabel('Amplitude'); title('Unit Ramp');

Results:

Observations: In this question we observed the plots of unit step, unit ramp and unit
impulse signal in MATLAB.
c. Generate a sine wave of 100Hz, sampled at 8KHz for a duration of 20ms. Repeat the
experiment for different sampling rates like 100Hz, 200Hz, 500Hz and 1KHz and
comment on results.
MATLAB Code:
clc; clear all; close all;
f = 100;

fs1 = 8000;
t1 = 0:1/fs1:0.02;
x1 = sin(2*pi*f*t1);

fs2 = 100;
t2 = 0:1/fs2:0.02;
x2 = sin(2*pi*f*t2);

fs3 = 200;
t3 = 0:1/fs3:0.02;
x3 = sin(2*pi*f*t3);

fs4 = 500;
t4 = 0:1/fs4:0.02;
x4 = sin(2*pi*f*t4);

fs5 = 1000;
t5 = 0:1/fs5:0.02;
x5 = sin(2*pi*f*t5);

subplot(511); stem(t1,x1);
xlabel('time(sec)');
ylabel('amplitude');
title('sine wave sampled at fs = 8KHz');

subplot(512); stem(t2,x2);
xlabel('time(sec)');
ylabel('amplitude');
title('sine wave sampled at fs = 100Hz');

subplot(513); stem(t3,x3);
xlabel('time(sec)');
ylabel('amplitude');
title('sine wave sampled at fs = 200Hz');

subplot(514); stem(t4,x4);
xlabel('time(sec)');
ylabel('amplitude');
title('sine wave sampled at fs = 500Hz');
subplot(515); stem(t5,x5);
xlabel('time(sec)');
ylabel('amplitude');
title('sine wave sampled at fs = 1KHz');

Results:

Observation: In this question we observed how to sample sine wave at different


frequencies and how they will look after sampling.
d. Generate a signal which is represented as sum of the following sinusoids:
x1(t) = 5 cos(2π500t),
x2(t) = 5 cos(2π1200t + 0.25π),
x3(t) = 5 cos(2π1800t + 0.5π)
MATLAB Code:
clc; clear all; close all;
fs = 5000;
t = 0:1/fs:0.02;
x1 = 5*cos(2*pi*500*t);
x2 = 5*cos(2*pi*1200*t + 0.25*pi);
x3 = 5*cos(2*pi*1800*t + 0.5*pi);
x = x1+x2+x3;
plot(t,x);
xlabel('time(sec)');
ylabel('amplitude');
title('sine wave');

Results:

Observations: In this question we observed how the sum of given signals will look like.
e. In the previously generated signal, add iid Gaussian noise with SNR of 20dB and plot the
signal. Hint: Use randn() function in MATLAB to generate noise.
MATLAB Code:
clc; clear all; close all;

fs = 5000;
as = 5;
snr_db = 20;
snr = 10^(snr_db/10);
an = as/snr;

t=0:1/fs:0.01;

x1 = as.*cos(2*pi*500*t);
x2 = as.*cos(2*pi*1200*t + 0.25*pi);
x3 = as.*cos(2*pi*1800*t + 0.5*pi);
x = x1+x2+x3;

n1 = an*randn(size(x));
n1 = n1/max(n1);
n = an*n1;

y = x + 0.5 * n;

plot(t,x); hold on;


plot(t,y,'--','linewidth',1.25); hold off;
xlabel('time(sec)');
ylabel('amplitude');

Results:

Observations: In this
question we observed the
effect of 20dB AWGN noise
on our previously generated
signal.
Conclusion: From this experiment I learnt about MATLAB like how to
write programs and run them, how to define matrices and arrays, how to
define unit step, unit ramp and unit impulse and plot their waveforms. I
learnt about three different types of plotting waveforms: plot, subplot and
stem. Then learnt about how to generate sine waves and sample them at
different frequencies. Also learnt about how to format your waveforms like
giving them title and naming x and y axis, increasing the width of line of
waves, hold on, hold off.

You might also like