0% found this document useful (0 votes)
68 views18 pages

MATLAB Basics: Commands & Signal Plots

The experiment aims to plot the magnitude and phase spectra of a signal using Fourier transforms in MATLAB. The code defines an exponential signal, takes its FFT, and plots the magnitude spectrum, phase spectrum, and original time domain signal for comparison. Taking the FFT of a signal provides its frequency domain representation in terms of magnitude and phase across different frequencies.

Uploaded by

Kumar Rajeshwar
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)
68 views18 pages

MATLAB Basics: Commands & Signal Plots

The experiment aims to plot the magnitude and phase spectra of a signal using Fourier transforms in MATLAB. The code defines an exponential signal, takes its FFT, and plots the magnitude spectrum, phase spectrum, and original time domain signal for comparison. Taking the FFT of a signal provides its frequency domain representation in terms of magnitude and phase across different frequencies.

Uploaded by

Kumar Rajeshwar
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

Experiment No.

– 1

Aim: Intro to MATLAB & its basic commands.


Software Used: MATLAB
Theory:
MATLAB is used for machine learning, signal processing,
image processing, computer vision, communications,
computational finance, control design, robotics, and
much more.
MATLAB platform is optimized for solving engineering
and scientific problems. The matrix-based MATLAB
language is the world’s most natural way to express
computational mathematics.
MATLAB code can be integrated with other languages,
enabling you to deploy algorithms and applications
within web, enterprise, and production systems.
MATLAB allows matrix manipulations, plotting of
functions and data, implementation of algorithms,
creation of user interfaces, and interfacing with
programs written in other languages.
Primary Commands:
Help: lists all primary help topics in the Command
Window. Each main help topic corresponds to a folder
name on the MATLAB search path.
Help name: displays the help text for the functionality
specified by name, such as a function, method, class,
toolbox or variable.
Plot: Draw a plot, see also figure, axis, subplot.
Print: Print the current plot (to a printer or postscript
file)
Subplot : Divide the plot window up into pieces, see
also plot, figure.
xlabel/ylabel: Add a label to the horizontal/vertical axis
of the current plot, see also title, text, gtext
OUTPUTS :-

Result:
The study of MATLAB and the various commands is
complete.
Experiment No. – 2
Aim: To plot unit step, unit impulse, ramp,exponential
functions & sinusoidal signals.
Software Used: MATLAB
Program:
%Generation of sine wave

t=1:0.01:5;
x=sin(2*pi*t);
subplot(3,2,1);
plot(t,x);
title('sine wave continuous');
xlabel('time');
ylabel('amplitude');

%Generation of sine wave


t=1:0.01:5;
x=sin(2*pi*t);
subplot(3,2,2);
stem(t,x);
title('sine wave discrete');
xlabel('time');
ylabel('amplitude');

%Generation of cos wave


t=0:0.01:5;
x=cos(2*pi*t);
subplot(3,2,3);
plot(t,x);
title('cos wave');
xlabel('time');
ylabel('amplitude');

%Generation of cos wave


t=0:0.01:5;
x=cos(2*pi*t);
subplot(3,2,4);
stem(t,x);
title('cos wave');
xlabel('time');
ylabel('amplitude');
%Generation of Ramp wave
N=input('Enter the value of N:');
n=0:N-1;
r=n;
subplot(3,2,5);
plot(n,r);
title('ramp signal');
xlabel('time');
ylabel('amplitude');

%Generation of Ramp wave


M=input('Enter the value of M:');
n=0:M-1;
r=n;
subplot(3,2,6);
stem(n,r);
title('ramp signal');
xlabel('time');
ylabel('amplitude');

%Generation of Exponential wave


N=input('Enter the value of N');
n=0:N-1;
a=input('Enter the value of a');
xn=a.^n;
subplot(2,2,1);
plot(n,xn);
title('exponential signal continuous');
xlabel('time');
ylabel('amplitude');

%Generation of Exponential wave


m=input('Enter the value of m');
n=0:m-1;
b=input('Enter the value of b');
xn=b.^n;
subplot(2,2,2);
stem(n,xn);
title('discrete exponential signal');
xlabel('time');
ylabel('amplitude');

%Generation of unit step function


t = (-1:0.01:5)';
unitstep = t>=0;
subplot(2,2,3);
plot(t,unitstep);
title('unit step function');
xlabel('time');
ylabel('amplitude');

%Generation of unit impulse


t = (-1:0.01:5)';
impulse = t==0;
subplot(2,2,4);
plot(t,impulse);
title('unit impulse function');
xlabel('time');
ylabel('amplitude');
OUTPUTS:-

Result:
Plotting of unit step, unit impulse, ramp, exponential
functions and sinusoidal signals using MATLAB software
is completed.
Experiment No. – 3
Aim: To plot Convolution of two functions.
Software Used: MATLAB
Theory:
Convolution of two functions mathematically is given
by: -
f(t)=x(t)*h(t)= ∫𝑥(𝜏)ℎ(𝑡−𝜏)𝑑𝜏∞−∞
Program:
%Definition of i/p sequence
x=input('Enter the value of x');
nx=0:1;
subplot(3,1,1);
stem(nx,x);
grid on;
title('Input Sequence');
xlabel('Time');
ylabel('Amplitude');
%Definition of impulse sequence h
h=input('Enter the value of h');
nh=0:2;
subplot(3,1,2);
stem(nh,h);
grid on;
title('Input impulse seq.');
xlabel('Time');
ylabel('Amplitude');

%Convolution of x & h
nyl=nx(1)+nh(1);
nyr=nx(length(x))+nh(length(h));
ny=nyl:nyr;
y=conv(x,h);
subplot(3,1,3);
stem(ny,y);
grid on;
title('output Sequence');
xlabel('Time');
ylabel('Amplitude');

Outputs:-
Result:
Study of convolution of two functions using MATLAB is
finished successfully.
Experiment No. – 4
Aim: To plot auto correlation and cross correlation of two
sequences..

Software Used: MATLAB


Theory:
Cross Correlation: In signal processing, cross-correlation is a measure of
similarity of two series as a function of the displacement of one relative to the
other. This is also known as a sliding dot product or sliding inner-product. It is
commonly used for searching a long signal for a shorter, known feature. It has
applications in pattern recognition, single particle analysis, electron
tomography, averaging, cryptanalysis, and neurophysiology

Auto Correlation: Autocorrelation, also known as serial correlation, is the


correlation of a signal with a delayed copy of itself as a function of delay.
Informally, it is the similarity between observations as a function of the time lag
between them. The analysis of autocorrelation is a mathematical tool for
finding repeating patterns, such as the presence of a periodic signal obscured by
noise, or identifying the missing fundamental frequency in a signal implied by its
harmonic frequencies. It is often used in signal processing for analyzing
functions or series of values, such as time domain signals. Unit root processes,
trend stationary processes, autoregressive processes, and moving average
processes are specific forms of processes with autocorrelation.
Program:
clear all ;
close all ;
clc;
%definition of x
x=[1 2 3 4];
subplot(4,1,1);
stem(x);
xlabel('time');
ylabel('amplitude');
title('Definition of x');
%definition of h
h=[4 3 2 1];
subplot(4,1,2);
stem(h);
title('Definition of h');
xlabel('time');
ylabel('amplitude');
%autocorrelation of x&x
y=xcorr(x,x);
subplot(4,1,3);
stem(y);
title('Autocorrelation');
xlabel('time');
ylabel('amplitude');
%crosscorrelation of x&h
y=xcorr(x,h);
subplot(4,1,4);
stem(y);
title('Crosscorrelation');
xlabel('time');
ylabel('amplitude');
Output:

Result:
Study of auto correlation and cross correlation of two
sequences using MATLAB is finished successfully.
Experiment No. – 5
Aim: Find out the Z transform of a signal.
Software Used: MATLAB
Theory:-
The Z-transform can be defined as either a one-sided or two-sided transform.

Bilateral Z-transform
The bilateral or two-sided Z-transform of a discrete-time signal x[n] is the formal power series X(z)
defined as
Program:
clc;
clear all;
n=0:.1:100;
r=input('enter the r :');
w=pi/4;
y1=r.^(-n);
y2=cos(w*n);
y=y1.*y2;
if (r <1)
subplot(3,1,1);
plot(n,y1);
xlabel('Sample No');
ylabel('amplitude');
title('r^-^n when r<1');
elseif(r==1)
subplot(3,1,1);
plot(n,y1);
xlabel('Sample No');
ylabel('amplitude');
title('r^-^n when r=1');
else
subplot(3,1,1);
plot(n,y1);
xlabel('Sample No');
ylabel('amplitude');
title('r^-^n when r>1');
end
subplot(3,1,2);
plot(n,y2);
xlabel('Sample No');
ylabel('amplitude');
title('cos(wt) when w=pi/4');
subplot(3,1,3);
plot (n,y);
xlabel(' ');
ylabel('amplitude');
title('Real part of z^-^n=r^-^n *cos(wn)');

Output:-
CASE 1: enter the r :0.9
CASE 2 :- enter the r :1

CASE 3:- enter the r :1.1


Experiment No. – 6
Aim: Evaluate the DTFS coefficients of a signal and plot them
Software Used: MATLAB
Code:-
clc;

clear all;
N=17;
n=[Link];
x=cos(6*pi*n/N + pi/3);
subplot(2,1,1);
stem(n,x); %Plot discrete function
y= fft(x); %Fourier series function
subplot(2,1,2);
stem(n,real(y));

OUTPUT:-
Experiment No. – 7
Aim: . Plot the spectra of ideally sampled signal w.r.t. sampling of
Discrete time signals.
Software Used: MATLAB
Code:-
clc;
clear;
f = 2000;
T = 1/f;
tmin = 0;
tmax = 5*T; % Plot 5 cycles
dt1 = 1/10000;
dt2 = 1/3000;
t1 = tmin:dt1:tmax;
t2 = tmin:dt2:tmax;
x1 = sin(2*pi*f*t1);
x2 = sin(2*pi*f*t2);
subplot(2,1,1);
stem(t1,x1);
subplot(2,1,2);
stem(t2,x2);

OUTPUT:-
Experiment No. – 8
Aim: Plot the magnitude and phase spectra of a signal using Fourier transforms
Software Used: MATLAB
Code:-
clc;
clear;
close all;
A=2;
a=4;
fs=1000; % Sampling frequency
t=0:1/fs:1; %Time
x=A*exp(-a.*t); %Signal
plot(t,x); %Plotting the time domain signal
xlabel('t');
ylabel('x(t)');
title('Time domain Signal')
N=length(x);
N1=2^nextpow2(N);
X=fft(x,N1);
X=X(1:N1/2); %Discard Half of Points
X_mag=abs(X); %Magnitude Spectrum
X_phase=phase(X); %Phase Spectrum
f=fs*(0:N1/2-1)/N1; %Frequency axis
figure
plot(f,(X_mag/N1)); %Plotting the Magnitude Spectrum after Normalization
xlabel('Frequency (Hz)');
ylabel('Magnitude Spectrum');
title('Magnitude Spectrum vs f');
figure
plot(f,X_phase); %Plotting the frequency domain
xlabel('Frequency (Hz)');
ylabel('Phase Spectrum');
title('Phase Spectrum vs f');

OUTPUT:-

Common questions

Powered by AI

Fourier transforms in MATLAB are fundamental for converting time-domain signals into their frequency-domain representations, which highlight the signal's frequency components through magnitude and phase spectra. This transformation enables engineers to identify and analyze the contribution of each frequency component, ensuring optimal signal processing applications such as noise reduction, signal compression, and filtering. MATLAB's 'fft' function calculates the Fourier transform, providing data for plotting magnitude and phase spectra using its robust graphing tools. The magnitude spectrum visualizes the signal's strength at various frequencies, while the phase spectrum indicates the phase shift of each frequency component. These plots are crucial for understanding signal behavior, designing systems with precise frequency responses, and diagnosing issues in signal transmission .

The 'subplot' function in MATLAB is used to create a grid of plots within a single figure window, which allows researchers to visualize multiple datasets or signals simultaneously. This is particularly useful for comparative analysis, enabling the simultaneous observation of relationships and differences between distinct datasets. In practice, 'subplot' is called with parameters specifying the number of rows and columns for the grid, and the specific location within the grid where each plot should be placed. This function is essential for efficiently presenting experimental results, such as comparing different types of signals like ramps, impulses, or periodic waves in one cohesive display .

MATLAB's capability to integrate with other languages, such as C, C++, Java, and Python, significantly enhances its application in deploying algorithms for web and enterprise systems by offering flexibility and interoperability. This allows MATLAB-developed algorithms to be seamlessly embedded into larger systems that might be written in other languages, facilitating automated workflows and reducing the need for extensive re-coding. Such integration makes it possible to utilize MATLAB's robust computational capabilities in diverse environments, thereby streamlining deployments in web, enterprise, and production systems while maintaining the performance and reliability of MATLAB algorithms .

Evaluating the DTFS coefficients in MATLAB involves computing the frequency domain representation of a discrete signal. The process starts with defining the signal in time domain andthen using the 'fft' function in MATLAB to obtain its frequency components. The DTFS coefficients are fundamental components indicating the amplitude and phase of constituent sinusoidal components at discrete frequencies. These coefficients are plotted to analyze the frequency content and signal periodicity over a defined interval. The purpose of evaluating DTFS coefficients lies in signal synthesis and analysis, allowing for efficient communication signal design, filtering, and reconstruction of the signal from its frequency components .

MATLAB provides a straightforward interface to facilitate the study of convolution through built-in functions and customizable plotting capabilities. In signal processing, convolution is crucial as it provides a mathematical operation that combines two signals to form a third signal, representing how the shape of one affects or modifies the other. This operation is vital for filtering applications, system analysis, and understanding the system's impulse response. Practically, MATLAB uses the 'conv' function to perform convolution, which leverages matrix operations to efficiently compute the result, while plotting these results can help visualize the effects and interactions between the contributing signals .

Using different sampling rates for discrete-time signals in MATLAB affects signal interpretation by influencing the level of detail captured from the original signal. Higher sampling rates preserve more information from the continuous signal, leading to more accurate digital representations and less aliasing. Conversely, lower sampling rates may result in loss of signal detail and potential misrepresentation of the signal's true characteristics. MATLAB's ability to define precise sampling rates allows engineers to balance between computational efficiency and accuracy, which is critical in applications like digital audio and telecommunications where accurate signal reconstruction is essential .

Autocorrelation and cross-correlation are techniques used to analyze signals. Autocorrelation measures the similarity of a signal with a delayed version of itself over varying time lags, which helps in detecting repeating patterns and identifying dominant frequencies within the signal. It is useful for noise reduction and identifying the signal's fundamental period. Cross-correlation, on the other hand, measures the similarity between two different signals as a function of time-lag of one relative to the other. It is commonly used in signal detection and pattern recognition to find if a certain known pattern is present in a longer, complex signal. Both techniques facilitate deeper analysis and understanding of signal characteristics in various applications like neurophysiology and telecommunications .

Plotting both continuous and discrete forms of sinusoidal signals in MATLAB is significant for understanding the differences in signal representation and processing across various applications. Continuous signals are visualized using the 'plot' function for smooth curves, while discrete signals are visualized with the 'stem' function to highlight individual sample points. This dual approach helps to compare and contrast the behavior of signals in both domains, offering insights into how signals are sampled and represented in digital systems. Practically, this is implemented by defining time vectors and using trigonometric functions like sine and cosine to generate the data for plotting .

MATLAB's matrix-based language allows for the natural expression of computational mathematics, which is particularly beneficial in engineering and scientific problem solving because it provides a concise and intuitive framework for handling large datasets and performing complex calculations. This approach reduces the need for iterative loops when performing matrix operations, making it easier to write and understand code that performs calculations across entire datasets at once. This capability is particularly advantageous for tasks in machine learning, signal processing, and computational finance, where matrix operations are frequent and essential .

The Z-transform in MATLAB is used for analyzing discrete-time signals and systems. It transforms a sequence of signals into a complex frequency domain representation, helping in the determination of system characteristics such as stability and frequency response. The Z-transform allows engineers to represent discrete signals algebraically, making it easier to handle and solve difference equations that describe system behavior. It is also crucial for designing digital filters where stable and accurate frequency responses are mandatory. MATLAB facilitates this with its ability to calculate and plot the Z-transform, offering an intuitive visual understanding through graphs that exhibit magnitude and phase characteristics .

You might also like