KEMBAR78
Signal Analysis Lab | PDF | Matlab | Matrix (Mathematics)
0% found this document useful (0 votes)
26 views36 pages

Signal Analysis Lab

This lab report presents an introduction to MATLAB, focusing on its capabilities for signal analysis and processing. It covers fundamental concepts such as dynamic typing, data structures, basic operations, control flow, and the distinction between continuous-time and discrete-time signals. The report includes exercises demonstrating the application of MATLAB functions for plotting various signals and understanding convolution in signal processing.

Uploaded by

077bel001.aayush
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)
26 views36 pages

Signal Analysis Lab

This lab report presents an introduction to MATLAB, focusing on its capabilities for signal analysis and processing. It covers fundamental concepts such as dynamic typing, data structures, basic operations, control flow, and the distinction between continuous-time and discrete-time signals. The report includes exercises demonstrating the application of MATLAB functions for plotting various signals and understanding convolution in signal processing.

Uploaded by

077bel001.aayush
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
You are on page 1/ 36

TRIBHUVAN UNIVERSITY

INSTITUTE OF ENGINEERING
PULCHOWK CAMPUS

A LAB REPORT ON SIGNAL ANALYSIS

SUBMITTED BY
AAYUSH JHA (077BEL001)

SUBMITTED TO
Department of Electronics and Computer
Engineering Pulchowk Campus, Institute of
Engineering Tribhuvan University
Lalitpur, Nepal

MARCH, 2024
LAB 1 : GETTING STARTED WITH MATLAB

BACKGROUND

MATLAB, an acronym for MATrix LABoratory, is a powerful software platform designed for
scientific computing and technical calculations. Unlike many traditional programming
languages, MATLAB emphasizes working with matrices and vectors, providing an intuitive
environment for manipulating numerical data.

Variables and Expressions

● Dynamic Typing: Unlike languages like Java or C++, MATLAB doesn't require pre
defining variable types. Simply assign a value using an expression, and MATLAB
automatically determines the type (e.g., a = sin(64) + 2).

● Implicit Variable Creation: If you don't provide a specific name, MATLAB generates the
temporary variable ans to hold the result (e.g., 3+2 results in ans = 5).

Data Structures

● Vectors: Ordered sequences of numbers, representing single-dimensional data (e.g., x =


[1:10]).

● Matrices: Rectangular arrays of numbers, representing multidimensional data (e.g., z =


[1:3; 4:6; 7:9]).

Basic Operations

● Arithmetic Operators: +, -, *, /, \, ^ for performing calculations on numerical data.

● Mathematical Functions: Built-in functions like ABS, SQRT, LOG, SIN, and COS for
trigonometric and other mathematical operations.
Control Flow

● Loops: FOR and WHILE loops for repeated execution of code blocks based on specific
conditions.

● Conditional Statements: IF ... ELSE statements for branching execution based on logical
tests.

More Topics
Dealing with wavread, wavwrite, >y=wavread('C:\sound.wav');%file must be
sound files auread, auwrite, valid >sound(y,44100);
sound(y,fsamp)

Complex j, real, imag, abs, > real(j) % locate a complex


numbers angle, number in cartesian form
> imag(j) > abs(j) % locate a complex
number in polar form; > angle(j)

Signal Fft(),dft(),conv() >>clear >>A=imread('my_pic.jpg'); %file


processing dither(),gray2ind(),in must be valid >>whos >>imshow(A)
and Image d2gray(),ind2rgb()
processing imread() ,imwrite( , )

Transfer Tf2zp Zp2tf Freqs() Given H(s)=(2s+3)(s3+4s2+5) >num[2 3]


function Semilogx() bode() >den=[1 4 0 5] > [z,p,k]=tf2zp(num,den) >
representat [num.den]=zp2tf(z,p,k) %one way of plotting
i on and >T=0:0.1:1; >Y=step(num,den,t); >Plot(t,y) %
frequency another way of plotting >Bode(num,den) >
response [mag,phase]=bode(num,den,w);
>Magdb=20*log10(mag)
>Semilogx(w,magdb) >Semilogx(w,phase)

Getting help from Matlab

> doc fft


> help help
> help cos
> help fft
> lookfor filter

Demonstration of scripting

1. To invoke scripts from matlab: write your own matlab file using emacs, xemacs or the
Matlab editor and save it as myfile.m
2. Type in the Matlab prompt myname
3. Use % as comments
4. ; To suppress output
EXERCISES

Code
f = @(n) (1 + 2/(n)^2)^n;
fprintf('f(3) = %s\n', num2str(f(3)));
fprintf('f(7) = %s', num2str(f(7)));

Output

Code

a = 2;
omega = 5;
t = linspace(0, 10, 1000);
y = exp(-a*t) .* cos(omega*t);
figure; plot(t, y);
title('\bf\color{red}\fontsize{10} 077BEL001 AAYUSH JHA: Plot of y = e^{-at}
cos(\omega t)');

xlabel('\bf\color{blue}\fontsize{30} Time (t)');


ylabel('\bf\color{blue}\fontsize{30} y');
grid on;
Output

Code

f = [0; 1];
evenCount = 0;
oddCount = 0;
i = 3;
maxValue = 1e4;

while true
nextTerm = f(end-1) + f(end);
if nextTerm > maxValue
break;
end

f(end+1) = nextTerm;

if rem(nextTerm, 2) == 0
evenCount = evenCount +
1;
else
oddCount = oddCount + 1;
end
i = i + 1;
end

disp(['Number of Even Terms: '


num2str(evenCount)]); disp(['Number of Odd Terms: '
num2str(oddCount)]);

plot(f);
xlabel('\fontsize{30}Index');
ylabel('\fontsize{30}Fibonacci Number');
title([num2str(length(f)) '-term Fibonacci Sequence 077BEL001']);
grid on;
legend({['First ', num2str(length(f)), ' Fibonacci Numbers']});

Output:
Code

x = 0:0.01:100;
f = (x.^2 + 2.*x + 3) ./ (x + 3);

figure;
plot(x, f);
title('\fontsize{10} 077BEL001 AAYUSH JHA Plot of f(x) = (x^2 + 2x + 3) / (x +
3)');
xlabel('\fontsize{20} x');
ylabel('\fontsize{20} f(x)');

Output
DISCUSSION

In this lab, we familiarized ourselves with the fundamentals of MATLAB, a robust software
platform designed for scientific computing. We explored basic data structures like vectors and
matrices, along with essential operations and mathematical functions.The lab also introduced
control flow structures, including loops and conditional statements, crucial for executing code
under specific conditions.
The MATLAB help system was highlighted for documentation and assistance, emphasizing
its role in aiding users. We also learned to use the plot and subplot commands to create plots
and subplots and customize the plots in various ways.

CONCLUSION

In conclusion, this lab served as a foundational exploration into MATLAB's capabilities. The
understanding of dynamic typing, data structures, basic operations, and control flow structures is
essential for leveraging MATLAB's power in scientific and technical computations.
LAB 2 : FAMILIARIZATION WITH BASIC CT/DT FUNCTIONS

BACKGROUND

The foundation of signal processing revolves around differentiating between continuous-time


(CT) and discrete-time (DT) signals. Continuous-time signals exist for all values within a given
time interval and are represented by functions of a continuous variable. This lab explores key CT
signals such as impulse response, unit step, ramp, and rectangular signals.

Discrete-time signals, on the other hand, are defined only at discrete time instances, often
obtained by sampling continuous-time signals. The lab investigates discrete-time exponential
functions, highlighting the discrete nature of certain signals.

To facilitate these explorations, an array of MATLAB functions is introduced. Understanding


functions such as who, whos, input(), disp(), subplot(), figure(), clear all, close all, home, hold
on, grid on, grid off, grid, demo, ver, lookfor, length(), pause, plot(), stem(), real(), imag(),
zeros(), ones(), exp(), along with control flow statements like for and if-else, is fundamental
to
signal analysis. These functions empower the creation, manipulation, and visualization of
signals, laying the groundwork for a deeper understanding of signal processing concepts.

EXERCISES

1. Plot the basic signal using Matlab

a) Impulse response

Code
x = -10:0.01:10;
impulse = x == 0;
figure;
stem(x, impulse);
title('\fontsize{10} Impulse Response , 077BEL001
Aayush Jha');
xlabel('\fontsize{20}Time');
ylabel('\fontsize{20} Amplitude');

Output
b) Unit

step Code

unit_step = x >= 0;
figure;
stem(x, unit_step);
title('\fontsize{10} Unit Step Response, 077BEL001 Aayush Jha');
xlabel('\fontsize{20} Time');
ylabel('\fontsize{20} Amplitude');

Output
c) Ramp

Code

% Ramp
ramp = x .* (x >= 0);
figure;
stem(x, ramp);
title('\fontsize{10} Ramp Response, 077BEL001 Aayush Jha');
xlabel('\fontsize{20} Time');
ylabel('\fontsize{20} Amplitude');

Output
d) Rectangular

Code
% Rectangular
rectangular = abs(x) <= 0.5;
figure;
plot(x, rectangular);
title('\fontsize{10} Rectangular , 077BEL001
Aayush Jha');
xlabel('\fontsize{20} Time');
ylabel('\fontsize{20} Amplitude');

Output
Code

t = -2:0.1:2;
f = @(c,a) c*exp(a*t);
% Positive
x1 = f(2,3);
% Negative
x2 = f(-1,-2);
subplot(2,1,1);
plot(t, x1);
title('\fontsize{10} x(t) = C*exp(at), C = 2, a = 3 : 077BEL001 Aayush Jha');
subplot(2,1,2);
plot(t, x2);
title('\fontsize{10} x(t) = C*exp(at), C = -1, a = -2 : 077BEL001 Aayush Jha');
Output

Code

t = -2:0.1:2;
C = 2;
a_imag = 4;
x = C * exp(1j*a_imag*t); figure;
subplot(2,1,1);
plot(t, real(x));
title('\fontsize{10} Real part of x(t) : 077BEL001 Aayush Jha');
xlabel('\fontsize{20} Time (t)');
ylabel('\fontsize{20} Amplitude');
subplot(2,1,2);
plot(t, imag(x));
title('\fontsize{10} Imaginary part of x(t) : 077BEL001 Aayush Jha');
xlabel('\fontsize{20} Time (t)');
ylabel('\fontsize{20} Amplitude');

Output
Code

t = -10:0.1:10;
C_abs = 1; theta = pi/4;
r_values = [0, -1, 1];
omega = 2;
for r = r_values
x = C_abs * exp(r * t) .* (cos(omega * t + theta) + 1i * sin(omega * t + theta));
figure('Position', [10, 10, 900, 600]);
plot(t, real(x)); %real part of the signal hold on;
plot(t, imag(x)); %imaginary part of the signal legend('Real part', 'Imaginary part');
title(['\fontsize{10} (077BEL001 Aayush Jha) Plot for x(t) = |C|e^{rt}[cos(ωot+θ)+jsin(ωot+θ)]
with r = ', num2str(r)]);
end

Output
● When r = 0, the signal is a pure sinusoid, indicating a constant amplitude over time.
● When r < 0, the signal decays over time, indicating a decreasing amplitude.
● When r > 0, the signal grows over time, indicating an increasing amplitude.

Code
% Define the parameters
n = 0:50;
a_magnitude = 0.9; theta = pi/4;
a = a_magnitude * exp(1j*theta);
x = a.^n;
figure; subplot(2,1,1);
stem(n, real(x), 'filled');
title('\fontsize{10} Real part of x[n] (077BEL001 Aayush Jha) ');
xlabel('\fontsize{20} n');
ylabel('\fontsize{20} Real(x[n])');
subplot(2,1,2);
stem(n, imag(x), 'filled');
title('\fontsize{10} Imaginary part of x[n] (077BEL001 Aayush Jha) ');
xlabel('\fontsize{20} n');
ylabel('\fontsize{20} Imag(x[n])');

Output
Code
C = [1/3, 1/2, 1/4, 1, 1/4, 1/2, 1/3];
t = 0:0.01:1;
x = zeros(size(t));
for k = -3:3
x = x + C(k+4)*exp(1j*2*pi*k*t);
end
figure;
plot(t, real(x));
title('\fontsize{10} Synthesized Signal from FS
Coefficients (077BEL001 Aayush Jha)');
xlabel('\fontsize{20} Time');
ylabel('\fontsize{20} Amplitude')

Output
5. Plot fundamental sinusoidal signal, its higher harmonics up to 5th harmonics and add all of
them to see the result. Comment on the result.

Code

t = 0:0.01:1;
f0 = 1;
total_signal = zeros(size(t));
figure('Position', [10, 10, 900, 600]);
for k = 1:5
harmonic_signal = sin(2*pi*k*f0*t); total_signal = total_signal +
harmonic_signal; %harmonic signal
subplot(6, 1, k);
plot(t, harmonic_signal);
title(['\fontsize{10} (077BEL001 Aayush Jha) Harmonic ', num2str(k)]);
xlabel('\fontsize{20} Time');
ylabel('\fontsize{10} Amplitude');
end
subplot(6, 1, 6);
plot(t, total_signal);
title('\fontsize{10} (077BEL001 Aayush Jha) Total Signal');
xlabel('\fontsize{20} Time');
ylabel('\fontsize{10} Amplitude');

Output

The first five plots show the fundamental sinusoidal signal and its higher harmonics up to the 5th
harmonics. The last plot shows the total signal, which is the sum of all the harmonic signals. As
you can see, the total signal has a more complex waveform than any of the individual harmonic
signals. This is because the total signal contains all the frequency components of the harmonic
signals, resulting in a waveform that is the superposition of all these signals.
DISCUSSION

In this lab, we delved into the essential functions of MATLAB, building a solid
foundation for future endeavors in signal processing and analysis. The exploration
began with a comprehensive understanding of various MATLAB functions, including
those for data representation, manipulation, and visualization. Key concepts such as
continuous-time (CT) and discrete-time (DT) signals were introduced, laying the
groundwork for subsequent signal processing tasks. We focused on functions for
drawing plots and subplots ,writing legends , labels and titles for the plots. We also saw
the use of other predefined functions for the understanding of CT/DT signals.

CONCLUSION

In conclusion, this lab served as an integral step in our journey to harness the
potential of MATLAB for signal processing
LAB 3 : DISCRETE AND CONTINUOUS TIME CONVOLUTION

BACKGROUND

Convolution, one of the most important concepts in engineering, can be used to determine the
output a system produces for a given input signal. It can be shown that a linear time invariant
system is completely characterized by its impulse response. The shifting property of the
continuous time impulse function tells us that the input signal to a system can be represented as
an integral of scaled and shifted impulses and, therefore, as the limit of a sum of scaled and
shifted approximate unit impulses. Thus, by linearity, it would seem reasonable to compute the
output signal as the limit of a sum of scaled and shifted unit impulse responses and, therefore, as
the integral of a scaled and shifted impulse response. That is exactly what the operation of
convolution accomplishes. Hence, convolution can be used to determine a linear time invariant
system's output from knowledge of the input and the impulse response.

Discrete Time Convolution

Convolution in discrete time is an operation that combines two discrete sequences to produce a
third sequence. It's a way of blending the information from one sequence with another, often
used in signal processing and digital systems.

Discrete convolution is widely used in signal processing, digital filtering, and system analysis.
Understanding discrete convolution is crucial for designing digital systems and processing
discrete signals.

Continuous Time Convolution

Continuous-time convolution combines two continuous-time signals, producing a third signal. It


is an integral operation representing the weighted average of the product of the two signals as
one is shifted over the other.

Continuous convolution is fundamental in understanding the behavior of linear time-invariant


systems and is essential in fields like control theory, communication systems, and physics.

EXERCISES
Continuous Time Convolution

Code

t = 0:0.1:10;
h = ones(1,11);
x = power(0.5,t);
y = conv(x,h) ;
subplot(311);
plot(x);
title('\fontsize{10} Continuous Time Convolutions (077BEL001 Aayush Jha)');
ylabel('\fontsize{20} x(t)')
subplot(312);
plot(h) ;
ylabel('\fontsize{20} h(t)');
subplot(313);
plot(y) ;
ylabel('\fontsize{20} y(t)');
xlabel('\fontsize{20} time');

Output
Discrete Set

Convolution Code

h = [1,0,0,1];
x = [1,2,-1,1];
y = conv(x,h);
subplot(311);
stem(x);
title('\fontsize{10} Discrete Set Convolutions (077BEL001 Aayush Jha)');
ylabel('\fontsize{20} x(n)');
subplot(312);
stem(h);
ylabel('\fontsize{20} h(n)');
subplot(313);
stem(y);
ylabel('\fontsize{20} y(n)');
xlabel('\fontsize{20} n');
Output

Discrete Time Convolution


Code
t = 0:11;
h = ones(1,11);
x = power(0.5,t);
y = conv(x,h);
subplot(311);
stem(x);
title('\fontsize{10} Discrete Time Convolutions (077BEL001 Aayush Jha)');
ylabel('\fontsize{20} x(t)');
subplot(312);
stem(h);
ylabel('\fontsize{20} h(t)');
subplot(313);
stem(y);
ylabel('\fontsize{20} y(t)');
xlabel('\fontsize{20} time');

Output:

DISCUSSION

In this lab, we implemented discrete and continuous-time convolutions using MATLAB.


Through practical exercises, we generated insights into the mathematical foundations and
practical applications of convolution, enhancing our understanding of signal processing and
system analysis. The comparison between discrete and continuous-time convolution underscored
their similarities and distinctions, emphasizing the versatility of convolution principles across
engineering domains.

CONCLUSION
In conclusion, this lab deepened our understanding of convolution. The hands-on experience
with MATLAB not only allowed us to apply convolution to real-world scenarios but also
provided valuable insights for future applications in engineering and signal processing.

LAB 4 : TRANSFER FUNCTION AND FREQUENCY RESPONSE

BACKGROUND

Transfer Function and Zeros/Poles:

Transfer Function (TF):


● The transfer function (TF) of a system serves as a fundamental tool in the analysis and
characterization of dynamic systems, particularly in the frequency domain.
Essentially, the TF encapsulates the intricate relationship between the system's input
and output signals, providing a concise yet comprehensive representation of the
system's behavior under various conditions.

Zeros and Poles:

● Zeros and poles constitute integral components of the transfer function, offering deeper
insights into the system's underlying characteristics. Zeros represent the input values
for which the transfer function becomes zero, essentially defining the points where the
system's output is null. On the other hand, poles are the values that result in the
denominator of the transfer function becoming zero, marking critical locations that
influence the system's stability and behavior.
.

MATLAB Functions:

● In the first part of the lab, the MATLAB functions tf2zp and zplane are utilized to
convert a transfer function defined by its numerator (num) and denominator (den)
coefficients into its zero-pole-gain representation. This representation allows
visualization of the system's zeros and poles in the complex plane.

Zero-Pole to Transfer Function:

● Conversely, the second part of the lab involves converting given zeros and poles into a
transfer function. The MATLAB function zp2tf is employed for this purpose.

Frequency Response:

Definition:

● The frequency response of a system serves as a powerful descriptor of how the system
reacts to sinusoidal inputs across a spectrum of frequencies. It is a comprehensive
representation, revealing not only the amplitude but also the phase shift of the
system's output signal in response to varying input frequencies. This information is
crucial for
understanding the system's behavior in the frequency domain, providing engineers with
valuable insights into the dynamic nature of the system.

MATLAB Function:
● In the final part of the lab, the MATLAB function freqz is used to calculate the frequency
response of a given system with numerator (num) and denominator (den) coefficients at
specified frequencies (w).

Visualization:
● The frequency response is then visualized by plotting the magnitude and phase of
the response.

EXERCISES

Transfer Function to Zero and poles.

Code

% TF to Zero and poles.


num = [1, 0.28];
den = [1, -1, 0.7];
[z, p,k] = tf2zp(num,den);
% Plot Zeros and Poles in the Complex Plane
zplane(z, p);
title('\fontsize{10} Zeros and Poles Plot (077BEL001 Aayush Jha)');

Output
Zero and poles to Transfer Function

Code

z =-0.28;
p=[
0.5+ 0.6708i,
0.5- 0.6708i,
];
k = 1;
[num,den] = zp2tf(z,p,k)

Output
Discrete Time Convolution

Code

% Frequency response
num = [0.008, -0.033, 0.05, -0.033, 0.008];
den = [1, 2.37, 2.7, 1.6, 0.4];
w = 20;
x = freqz(num,den,w); subplot(211); plot(abs(x));
title('\fontsize{10} Frequency Response (077BEL001 Aayush Jha)');
ylabel('\fontsize{20} abs(x)');
subplot(212);
plot(angle(x));
ylabel('\fontsize{20} angle(x)');
xlabel('\fontsize{20} z');

Output:
DISCUSSION

In this lab, we delved into transfer functions, zeros, poles, and frequency responses. The first
part of the lab focused on converting a transfer function, specified by its numerator and
denominator coefficients, into its zero-pole-gain representation using the tf2zp function.
Visualizing these zeros and poles in the complex plane using zplane provided valuable
insights into the system's stability and behavior. Adding a title to the plot enhanced the clarity
of the representation.

Moving on, the second part demonstrated the reverse process of converting given zeros and poles
back into a transfer function using the zp2tf function. This exercise emphasized the bidirectional
relationship between transfer functions and their zero-pole-gain representations.

Finally, the lab explored the frequency response of a system using the freqz function. The given
numerator and denominator coefficients were employed to calculate the frequency response,
which was then visualized through plots of magnitude and phase. Understanding how a system
responds to different frequencies is crucial in various engineering applications, and MATLAB
facilitated this analysis effectively.

CONCLUSION

In conclusion, this lab provided a comprehensive exploration of transfer functions, zeros, poles,
and frequency responses providing valuable insights for system analysis and design..

You might also like