0% found this document useful (0 votes)
17 views10 pages

Lab2 (1) 2

The document contains MATLAB code that generates and plots signals with different sampling frequencies. It: 1) Defines a continuous signal and plots it. 2) Generates discrete signals by sampling the continuous signal at different frequencies (10Hz, 15Hz, 20Hz, etc.) and plots them together in a tiled layout for comparison. 3) Samples the continuous signal at the given frequency of 300Hz and plots the discrete signal. So in summary, it demonstrates the effect of sampling frequency on a continuous signal by generating and comparing discrete signals from sampling at various rates.

Uploaded by

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

Lab2 (1) 2

The document contains MATLAB code that generates and plots signals with different sampling frequencies. It: 1) Defines a continuous signal and plots it. 2) Generates discrete signals by sampling the continuous signal at different frequencies (10Hz, 15Hz, 20Hz, etc.) and plots them together in a tiled layout for comparison. 3) Samples the continuous signal at the given frequency of 300Hz and plots the discrete signal. So in summary, it demonstrates the effect of sampling frequency on a continuous signal by generating and comparing discrete signals from sampling at various rates.

Uploaded by

amani2002aaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q1:

clc
clear
close all

SamplingFrequency = 300;
SamplingInterval = 0:1/SamplingFrequency:10 ; %% Time period = 1/Fs where FS
is the sampling frequency

FrequencyValue1 = 0 ; %% This is the variable holding the first frequency


value
FrequencyValue2 = 10 ; %% This is the variable holding the second frequency
value
FrequencyValue3 = 20 ; %% This is the variable holding the third frequency
value

x = 2 + 6*cos(20*pi*SamplingInterval) + cos(40*pi*SamplingInterval); %% This


is the variable holding the signal

plot(SamplingInterval,x);%% plotting the continuous signal

axis([0 0.2 -5.0 10]) %% limit the range

xlabel('Sampling Interval (s)') %% labeling the X axis

ylabel('Amplitude (V)') %% labeling the y axis

title('Continuous Signal') %% labeling the title

Figure 1
Q2: Part A
clc

clear
close all

SamplingFrequency = 300;
TimeVector = 0:1/SamplingFrequency:10 ; %% Time period = 1/Fs where FS is the
sampling frequency

x = 2 + 6*cos(20*pi*TimeVector) + cos(40*pi*TimeVector); %% This is the


variable holding the signal

plot(TimeVector,x);%% plotting the continuous signal

axis([0 0.2 -5.0 10]) %% limit the range

FsA = 10;
TimeVectorA = 0:1/FsA:10 ; %% Time period = 1/Fs where FS is the sampling
frequency
xA = 2 + 6*cos(20*pi*TimeVectorA) + cos(40*pi*TimeVectorA); %% This is the
variable holding the signal

FsB = 15;
TimeVectorB = 0:1/FsB:10 ; %% Time period = 1/Fs where FS is the sampling
frequency
xB = 2 + 6*cos(20*pi*TimeVectorB) + cos(40*pi*TimeVectorB); %% This is the
variable holding the signal

FsC = 20;
TimeVectorC = 0:1/FsC:10 ; %% Time period = 1/Fs where FS is the sampling
frequency
xC = 2 + 6*cos(20*pi*TimeVectorC) + cos(40*pi*TimeVectorC); %% This is the
variable holding the signal

FsD = 40;
TimeVectorD = 0:1/FsD:10 ; %% Time period = 1/Fs where FS is the sampling
frequency
xD = 2 + 6*cos(20*pi*TimeVectorD) + cos(40*pi*TimeVectorD); %% This is the
variable holding the signal

FsE = 60;
TimeVectorE = 0:1/FsE:10 ; %% Time period = 1/Fs where FS is the sampling
frequency
xE = 2 + 6*cos(20*pi*TimeVectorE) + cos(40*pi*TimeVectorE); %% This is the
variable holding the signal

tiledlayout(3,2); %% Using the command 'tiledlayout' to plot the 6 figures instead


of subplot

% Tile 1
nexttile
plot(TimeVectorA,xA);
axis([0 0.2 -5.0 10]) %% limit the range
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Analog signal for Fs = 10')

% Tile 2
nexttile
plot(TimeVectorB,xB);
axis([0 0.2 -5.0 10]) %% limit the range
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Analog signal for Fs = 15')

% Tile 3
nexttile
plot(TimeVectorC,xC);
axis([0 0.2 -5.0 10]) %% limit the range
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Analog signal for Fs = 20')

% Tile 4
nexttile
plot(TimeVectorD,xD);
axis([0 0.2 -5.0 10]) %% limit the range
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Analog signal for Fs = 40')

% Tile 5
nexttile
plot(TimeVectorE,xE);
axis([0 0.2 -5.0 10]) %% limit the range
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Analog signal for Fs = 60')

% Tile 6
nexttile
plot(TimeVector,x);
axis([0 0.2 -5.0 10]) %% limit the range
xlabel('Time (s)')
ylabel('Amplitude (V)')
title('Analog signal for Fs = 300')
Q2: Part B
clc
clear
close all
figure
tiledlayout(3,2); % Using the command 'tiledlayout' to plot the 6 figures instead
of subplot

FsA = 10; % First given frequency


FsB = 20; % Second given frequency
SamplingFrequency = 300; % Given sampling frequency
TimePeriod = 1/SamplingFrequency; % The time period is given by 1 / the sampling
frequency

T = 0:TimePeriod:10-TimePeriod; % Defining the step function


X = 2 + 6*cos(2*T*FsA*pi) + cos(2*T*pi*FsB); % This will generate the original
wave

plot(T,X); % this function displays the plot


xlabel('Time (seconds)'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
title(" Fs = 300 Hz"); % labels the title of the plot
axis([0 0.2 -6 10]); % defines the axis interval (range)

Fs1 = 10; % first freqency


Samplingperiod = 1/Fs1; % Sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; % Time interval based on
sampling period
Eq1 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % This is equation
1 which we will use to plot the signal
Signal_1 = 1:10*Fs1; % displays first signal
nexttile; % displays the next figure
stem(Signal_1,Eq1); % displays the first discrete figure using stem
title('Discrete signal for F = 10 Hz'); % labels the title of the plot
xlabel('Sample Number'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
axis([0 0.2*Fs1 -3 10]); % displays the graph visualization

Fs2 = 15; % second freqency


Samplingperiod = 1/Fs2; % Sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; % Time interval based on
sampling period
Eq2 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generates the
second wave
Signal_2 = 1:10*Fs2; % displays the second signal
nexttile; % displays the next figure
stem(Signal_2,Eq2); % displays the second discrete figure
title('Discrete signal for F = 15 Hz'); % labels the title of the plot
xlabel('Sample Number'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
axis([0 0.2*Fs2 -5 11]); % displays the graph visualization

Fs3 = 20; % third freqency


Samplingperiod = 1/Fs3; % Sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; % Time interval
Eq3 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generates the
third wave
Signal_3 = 1:10*Fs3;% displays third signal
nexttile; %display the next figure
stem(Signal_3,Eq3);% display the third discrete figure
title('Discrete signal for F = 20 Hz'); % title of the plot
xlabel('Sample Number'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
axis([0 0.2*Fs3 -5 10]);% visulaize the graph

Fs4 = 40; %fourth freqency


Samplingperiod = 1/Fs4; %sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; %time interval
Eq4 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generate the
forth wave
Signal_4 = 1:10*Fs4;% display the fourth signal
nexttile;% display the next figure
stem(Signal_4,Eq4);% display the fourth discrete figure
title('Discrete signal for F = 40 Hz'); % title of the plot
xlabel('Sample Number'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
axis([0 0.2*Fs4 -10 10]); % visulaize the graph

Fs5 = 60; % fifth freqency


Samplingperiod = 1/Fs5; % sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; %time interval
Eq5 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generate the
fifth wave
Signal_5 = 1:10*Fs5;% display the fifth signal
nexttile; % displays the next figure
stem(Signal_5,Eq5); % displays the fifth discrete figure
title('Discrete signal for F = 60 Hz'); % labels the title of the plot
xlabel('Sample Number'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
axis([0 0.2*Fs5 -15 15]); % visulaizes the graph

Signal = 1:10*SamplingFrequency;% display the sampling signal


nexttile; %displays the next figure
stem(Signal,X); % displays the sampling discrete figure
title('Discrete signal for 300 Hz'); % labels the title of the plot
xlabel('Sample Number'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
axis([0 0.2*SamplingFrequency -6 10]); % displays the graph visualization
Q3
clc
clear
close all

figure
tiledlayout(3,2); % Using the command 'tiledlayout' to plot the 6 figures instead
of subplot

FsA = 10; % First given frequency


FsB = 20; % Second given frequency
SamplingFrequency = 300; % Given sampling frequency
TimePeriod = 1/SamplingFrequency; % The time period is given by 1 / the sampling
frequency

T = 0:TimePeriod:10-TimePeriod; % Defining the step function


X = 2 + 6*cos(2*T*FsA*pi) + cos(2*T*pi*FsB); % This will generate the original
wave

plot(T,X); % this function displays the plot


xlabel('Time (seconds)'); % labels the x-axis
ylabel('Amplitude (volts)'); % labels the y-axis
title(" Fs = 300 Hz"); % labels the title of the plot
axis([0 0.2 -6 10]); % defines the axis interval (range)

Fs1 = 10; % first freqency


Samplingperiod = 1/Fs1; % Sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; % Time interval based on
sampling period
Eq1 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % This is equation
1 which we will use to plot the signal
Signal_1 = 1:10*Fs1; % displays first signal
nexttile; % displays the next figure
stem(Signal_1,Eq1); % displays the first discrete figure using stem
title('F = 10 Hz'); % labels the title of the plot
axis([0 0.2*Fs1 -6 10]); % displays the graph visualization

Fs2 = 15; % second freqency


Samplingperiod = 1/Fs2; % Sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; % Time interval based on
sampling period
Eq2 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generates the
second wave
Signal_2 = 1:10*Fs2; % displays the second signal
nexttile; % displays the next figure
stem(Signal_2,Eq2); % displays the second discrete figure
title('F = 15 Hz'); % labels the title of the plot
axis([0 0.2*Fs2 -5 11]); % displays the graph visualization

Fs3 = 20; % third freqency


Samplingperiod = 1/Fs3; % Sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; % Time interval
Eq3 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generates the
third wave
Signal_3 = 1:10*Fs3;% displays third signal
nexttile; %display the next figure
stem(Signal_3,Eq3);% display the third discrete figure
title('F = 20 Hz'); % title of the plot
axis([0 0.2*Fs3 -5 10]);% visulaize the graph

Fs4 = 40; %fourth freqency


Samplingperiod = 1/Fs4; %sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; %time interval
Eq4 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generate the
forth wave
Signal_4 = 1:10*Fs4;% display the fourth signal
nexttile;% display the next figure
stem(Signal_4,Eq4);% display the fourth discrete figure
title('F = 40 Hz'); % title of the plot
axis([0 0.2*Fs4 -10 10]); % visulaize the graph

Fs5 = 60; % fifth freqency


Samplingperiod = 1/Fs5; % sampling period
TimeInterval = 0:Samplingperiod:10-Samplingperiod; %time interval
Eq5 = 2 + 6*cos(20*TimeInterval*pi) + cos(40*TimeInterval*pi); % Generate the
fifth wave
Signal_5 = 1:10*Fs5;% display the fifth signal
nexttile; % displays the next figure
stem(Signal_5,Eq5); % displays the fifth discrete figure
title('F = 60 Hz'); % labels the title of the plot
axis([0 0.2*Fs5 -15 15]); % visulaizes the graph

Signal = 1:10*SamplingFrequency;% display the sampling signal


nexttile; %displays the next figure
stem(Signal,X); % displays the sampling discrete figure
title('300 Hz'); % labels the title of the plot
axis([0 0.2*SamplingFrequency -6 10]); % displays the graph visualization

figure; % visulaize figure


tiledlayout(3,2); % display the figure

nexttile % displays the next figure


pwelch(Eq1,[],[],[],Fs1); % displays the power spectrum of the first freqency
title('Fs = 10'); % labels the title of the plot

nexttile % displays the next figure


pwelch(Eq2,[],[],[],Fs2); % displays the power spectrum of the second freqency
title('Fs = 15'); % labels the title of the plot

nexttile % displays the next figure


pwelch(Eq3,[],[],[],Fs3); % displays the power spectrum of the third freqency
title('Fs = 20'); % labels the title of the plot

nexttile % displays the next figure


pwelch(Eq4,[],[],[],Fs4); % displays the power spectrum of the fourth freqency
title('Fs = 40'); % labels the title of the plot

nexttile % displays the next figure


pwelch(Eq5,[],[],[],Fs5);
title('Fs = 60');

nexttile
pwelch(X,[],[],[],SamplingFrequency);

You might also like