0% found this document useful (0 votes)
62 views27 pages

Dsap Lab Report 077bei045

Uploaded by

shardulkhanal491
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)
62 views27 pages

Dsap Lab Report 077bei045

Uploaded by

shardulkhanal491
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/ 27

TRIBHUVAN UNIVERSITY

INSTITUTE OF ENGINEERING PULCHOWK CAMPUS


Digital Signal Analysis and Processing

All Lab Reports Combined

Submitted By: Submitted To:


Name: Sujan Basnet Department of Electronics
Group: B and Computer Engineering
Roll No: 077BEI045
Lab 1 - Basic Signals
Objectives
• To understand how to use MATLAB / Octave
• To plot basic signals in Octave

Theory
Octave, also known as GNU Octave, is a programming language designed for
numerical computations. It offers a command-line interface for solving linear
and nonlinear problems, with compatibility similar to MATLAB. Key features
include efficient matrix operations, function plotting, and data visualization.
Octave serves as a free alternative for numerical computation tasks
Characteristics:
• Symbolic Math: Octave supports symbolic computations, allowing you
to manipulate algebraic expressions, perform differentiation,
integration, and solve equations symbolically.
• Built-in Functions: Octave provides a wide range of built-in functions for
mathematical operations, signal processing, statistics, and more. These
functions simplify complex tasks and enhance productivity.
• Extensibility: Octave allows you to create custom functions and
packages, extending its functionality beyond the built-in capabilities.
You can share your custom functions with others or reuse existing
packages.
• Parallel Computing: Octave supports parallel execution, enabling faster
computations by distributing tasks across multiple cores or processors.
• File I/O: Octave can read and write data from various file formats (e.g.,
CSV, HDF5, MAT) seamlessly, making it convenient for data analysis and
manipulation.

1
Programs

Sinusoid Signals
t = -pi : 0.01 : pi; y =
sin(t); x = cos(t); plot(t, y,
’r’); hold on; plot(t, x, ’g’);
xlabel(’t’); ylabel(’x(t)’);
title(’Sinusoid’);

Output

Figure 1: Sinusoid Functions : Sine and Cosine

Unit Step Function


hold on; for(n = -
10:10) if(n < 0)
stem(n, 0);
else stem(n, 1);
end;
end; hold
off;

2
Output

Figure 2: Discrete Unit Step Function

Ramp Signal
n = -10 : 10;
c = input("Enter the intercept: "); m =
input("Enter the slope: "); y = m*n + c;

stem(n, y, ’r’); xlabel("n");


ylabel("y[n]"); title("Ramp
signal")

3
Output

Figure 3: Ramp Function

Sinc Signal

clear all;

t = -5 : 0.01 : 5; x =
sinc(t);

plot(t, x); xlabel("t");


ylabel("sinc(t)"); title("Sinc
Function");

4
Output

Figure 4: Sinc Function

Exponential Signal

n = -5 : 5;
a = input("Enter three values for a: "); c = input("Enter the
amplitude scaling factor: ");

y = c*exp(a(1)*n); subplot(311);
stem(n, y); xlabel("n");
ylabel("y[n]"); title("Exponential
signal");

y = c*exp(a(2)*n); subplot(312);
stem(n, y); xlabel("n");
ylabel("y[n]"); title("Exponential
signal");

y = c*exp(a(3)*n);
subplot(313); stem(n, y);
xlabel("n");
ylabel("y[n]");

5
title("Exponential signal");
Output

Figure 5: Exponential Functions

Discussion
In the lab exercise, we utilized Octave to create plots of fundamental signals
such as step functions, ramps, sinusoids, and exponentials. We employed the
plot function for continuous signals and the stem function for discrete
signals. This introduction lays the groundwork for more advanced labs,
familiarizing students with Octave’s essential features.

Conclusion
Hence, this lab session was successfully completed by generating and plotting
basic signals in Octave.

6
Lab 2 - Convolution
Objectives
• To understand convolution of finite signals and calculate it in Octave

• To understand convolution of infinite signals and calculate it in Octave

Theory
Convolution is a mathematical operation used in signal processing, image
processing, and various other fields of engineering and applied mathematics.
It combines two functions to produce a third function that expresses how the
shape of one is modified by the other. Convolution is fundamental in the
analysis of linear time-invariant systems and is widely used in the context of
filtering, system analysis, and solving differential equations.

Mathematical Definition

The convolution of two continuous functions f(t) and g(t) is defined as:


For discrete functions f[n] and g[n], the convolution is defined as:

X
(f ∗ g)[n] = f[k]g[n − k]
k=−∞

Key Properties

• Commutativity:

f∗g=g∗f

• Associativity:
(f ∗ g) ∗ h = f ∗ (g ∗ h)

• Distributivity:

f ∗ (g + h) = (f ∗ g) + (f ∗ h)

• Identity Element: The delta function δ(t) acts as an identity element for
convolution:
7
f∗δ=f

• Time Shifting: If f(t) is shifted by t0, the convolution is also shifted:


f(t − t0) ∗ g(t) = (f ∗ g)(t − t0)
Applications

• Signal Processing: Convolution is used to filter signals, remove noise, and


detect features in time-series data.

• Image Processing: Convolution is applied in tasks such as edge detection,


blurring, and sharpening of images.

• System Analysis: Convolution helps in analyzing the response of linear


time-invariant systems to various inputs.

• Solving Differential Equations: Convolution is used to find particular


solutions to linear differential equations with non-homogeneous terms.

Programs

Convolution of Finite Signals


x = [1, 0, 0, 1]; h = [1 2
-1 1]; y = conv(x, h);

subplot(311); stem(x);
title("x[n]");

subplot(312); stem(h);
title("h[n]");

subplot(313); stem(y); title("y[n] =


x[n] * h[n]");

8
Output

Figure 6: Convolution of two Finite Signals

Convolution of Infinite Signals


a = input("Enter a value between 0 and 1: "); n = 0 : 19;
n_out = 0 : (2*20 - 1 - 1); u = ones(1, 20);

x = a.ˆn .* u;h = u
y = conv(x, h);

subplot(311); stem(n, x);


title("x[n]");

subplot(312); stem(n, h);


title("h[n]");

subplot(313); stem(n_out, y);


title("y[n] = x[n] * h[n]");

9
Output

Figure 7: Convolution of two Infinite Signals

Discussion
During the lab exercise, we employed Octave’s conv function to compute the
convolution of two functions. When dealing with functions of finite lengths,
the process was straightforward, as the conv function directly provided the
finite-length output. However, for functions with infinite length, we set a
limit (in this case, 20) and calculated the convolution within that range.

Conclusion
Hence, this lab session, was completed by calculating and plotting the
convolution between two finite and two infinite signals in Octave.

10
Lab 3 - Signal Transformation
Objectives
• To understand the use of fft and ifft to calculate Fourier transorm and its
inverse
• To calculate the Z-transform of a real signal and plot its zeroes and poles

FFT
The Fast Fourier Transform (FFT) is an efficient algorithm to compute the
Discrete Fourier Transform (DFT) and its inverse. The DFT is a mathematical
technique used to convert a discrete-time signal into its frequency
components. The FFT significantly reduces the computational complexity of
evaluating the DFT, making it practical for a wide range of applications in signal
processing, image processing, and more.

Mathematical Definition

The DFT of a discrete sequence x[n] of length N is given by:


N−1

X[k] = X x[n]e−j2Nπkn for k = 0,1,...,N − 1


n=0

The inverse DFT (IDFT) is defined as:

x[n] = 1 NX−1X[k]ej2Nπkn for n = 0,1,...,N − 1


N k=0
The FFT algorithm reduces the complexity from O(N2) to O(N logN).
Key Properties

• Linearity:
F{af[n] + bg[n]} = aF{f[n]} + bF{g[n]}

• Time Shifting:

F{f[n − n0]} = e−j2Nπkn0F{f[n]}

• Frequency Shifting:

11
F{ej2Nπk0nf[n]} = F[k − k0]

• Convolution Theorem: The convolution of two sequences in the time


domain corresponds to multiplication in the frequency domain:
F{f[n] ∗ g[n]} = F{f[n]} · F{g[n]}

Theory of Z-Transform
The Z-transform is a mathematical tool used in the analysis and
characterization of discrete-time signals and systems. It provides a means to
represent discrete-time signals in the frequency domain and is particularly
useful in the study of linear, time-invariant (LTI) systems.

Mathematical Definition

The Z-transform of a discrete-time signal x[n] is defined as:

X n
n=−∞

where z is a complex variable. The


inverse Z-transform is given by:

x[n] = 2πj1 IC X(z)zn−1 dz

where C is a counterclockwise closed contour encircling the origin in


the complex plane.

Key Properties

• Linearity:

Z{af[n] + bg[n]} = aZ{f[n]} + bZ{g[n]}

• Time Shifting:

Z{f[n − n0]} = z−n0Z{f[n]}

12
• Convolution: The convolution of two sequences in the time domain
corresponds to multiplication in the Z-domain:

Z{f[n] ∗ g[n]} = Z{f[n]} · Z{g[n]}

• Initial Value Theorem:

x[0] = zlim→∞X(z)

• Final Value Theorem:

nlim→∞x[n] = zlim→1(z − 1)X(z)

Programs

Calculation of FFT

x = [0 1 2 3]; y =
fft(x, 4);

subplot(311); stem(x);
xlabel("n"); ylabel("x[n]");
title("Original signal");

subplot(312); stem(real(y));
xlabel("n"); ylabel("Real part of
FFT"); title("FFT of the signal");

subplot(313); stem(imag(y));
xlabel("n"); ylabel("Img part of
FFT"); title("FFT of the signal");

13
Output

Figure 8: Fourier Transform using FFT

Different Parts of the FFT Output


x = [1.2 3.7 0.8 1 2.1]; y = fft(x,
8);

subplot(511); stem(x);
xlabel("n"); ylabel("x[n]");
title("Original signal");

subplot(512); stem(real(y));
xlabel("n"); ylabel("Real part of
FFT"); title("FFT of the signal");

subplot(513); stem(imag(y));
xlabel("n"); ylabel("Img part of
FFT"); title("FFT of the signal");

subplot(514); stem(abs(y));
xlabel("n"); ylabel("Magnitude of
FFT"); title("FFT of the signal");

subplot(515); stem(angle(y));
xlabel("n"); ylabel("Phase of FFT");
title("FFT of the signal");

14
Output

Figure 9: Different Parts of a FFT

Inverse FFT
x = [1.2 3.7 0.8 1 2.1]; y = fft(x,
8); x_recovered = ifft(y, 8);

subplot(311); stem(real(y));
xlabel("n"); ylabel("Real part of
FFT"); title("FFT Input");

subplot(312); stem(imag(y));
xlabel("n"); ylabel("Img part of
FFT"); title("FFT Input");

subplot(313); stem(x_recovered);
xlabel("n"); ylabel("x[n]");
title("Recovered Time Domain Signal");

15
Output

Figure 10: Getting the original signal using inverse FFT

Z-Transform
num = [1 0.8 0 2 0 3]; den = [0.76 0
2 1.7 2.3 1]; [z, p, k] = tf2zp(num,
den); zplane(z, p); title("Z-plane
Plot of Zeroes and Poles");

[num2, den2] = zp2tf(z, p, k); num =


num2/k den = den2/k
Output

Figure 11: Z Plane plot of the system

16
Discussion
In the lab exercise, we explored various signal and system transformations.
Specifically:
• We computed the Fourier transform of given signals using the Fast
Fourier Transform (FFT) algorithm.
• To recover the original signal, we applied the inverse FFT.
• Additionally, for a given digital system, we visualized its poles and
zeros in the z-domain.

Conclusion
Hence, this lab session was successfully completed by calculating and plotting
the Fourier transform of a signal using FFT (and the inverse) and plotting a
digital system in z domain.

17
Lab 4 - System Response
Objectives
• To plot the frequency response of a given system.
• To plot the frequency response of a given difference equation.

Theory
Digital systems are used to process discrete-time signals and are fundamental
in modern electronic applications such as computing, communications, and
control systems. These systems can be analyzed and designed using
mathematical tools such as transfer functions, which describe the input-
output relationship of a system in the frequency domain.

Transfer Function
The transfer function of a digital system characterizes the system’s response
to an input signal. It is defined in the Z-domain as the ratio of the Z-transform
of the output signal to the Z-transform of the input signal, assuming all initial
conditions are zero.

H(z) = XY ((zz))

where H(z) is the transfer function, Y (z) is the Z-transform of the

output signal, and X(z) is the Z-transform of the input signal.


Difference Equations
Digital systems are often described by linear constant-coefficient difference
equations. For a causal linear time-invariant (LTI) system, the difference
equation relating the input x[n] and output y[n] is given by:

y[n] + a1y[n − 1] + a2y[n − 2] + ··· + aNy[n − N]


= b0x[n] + b1x[n − 1] + b2x[n − 2] + ··· + bMx[n − M] Taking the

Z-transform of both sides, we obtain:

Y (z)1 + a1z−1 + a2z−2 + ··· + aNz−N


18
= X(z)b0 + b1z−1 + b2z−2 + ··· + bMz−M The transfer

function H(z) is then:

H(z) = XY ((zz)) = b1 +0 +ab11zz−−11++ab22zz−−22 ++ ······ ++


baMNzz−−NM

Poles and Zeros


The behavior of a digital system can be analyzed by examining the poles and
zeros of its transfer function.

• Zeros: The values of z that make the numerator of H(z) zero. These
represent frequencies where the output is minimized.

• Poles: The values of z that make the denominator of H(z) zero. These
represent frequencies where the system’s response is theoretically
infinite.

The locations of poles and zeros in the Z-plane provide insights into the
stability and frequency response of the system.

Stability
A digital system is stable if all the poles of its transfer function lie inside the
unit circle in the Z-plane. This means that for all poles zi:

|zi| < 1

Frequency Response
The frequency response of a digital system describes how the system
responds to different frequency components of an input signal. It is obtained
by evaluating the transfer function on the unit circle z = ejω:

H(ejω) = H(z)
z=ejω

where ω is the normalized angular frequency.

19
Applications of Transfer Functions in Digital Systems

• Filter Design: Transfer functions are used to design digital filters that can
pass, attenuate, or stop specific frequency components of a signal.

• System Analysis: Transfer functions help in analyzing the stability and


performance of digital systems, particularly in control systems and signal
processing.

• Signal Processing: Transfer functions are applied in various signal


processing techniques, including convolution, correlation, and spectral
analysis.

• Control Systems: Transfer functions are used in the design and analysis of
digital control systems, ensuring desired performance and stability
characteristics.

Programs
System Response
kg load signal;

w = 20;
## num = [0.008, 1.2, 3.5, 0, 4];
## den = [1, 2.5, 0, 1, 0.65]; ## x =
freqz(num, den, w);

p = [0.23 + 1.26j, 0.23 + 0.26j, -0.24 + 0.87j]; z = [0.67 + 0.49j,


0.67 - 0.49j]; k = 1;
[num, den] = zp2tf(p, z, k); x =
freqz(num, den, w);

subplot(211); plot(abs(x));
title("Magnitude Response")
xlabel("w"); ylabel("Mag");

subplot(212); plot(angle(x));
title("Phase Response")
xlabel("w"); ylabel("Phase");

20
Output

Figure 12: Response of a system with given Transfer Function

Difference Equation
## For the system described by the following difference equation, plot the
magnitude and phase response: ## y(n) - 0.67y(n-1) + 0.2y(n-2) = 0.32x(n) + x(n-1)

num = [0.32, 1]; den = [1, -


0.67, 0.2]; w = 20; x =
freqz(num, den, w);

subplot(211); plot(abs(x));
title("Magnitude Response")
xlabel("w"); ylabel("Mag");

subplot(212); plot(angle(x));
title("Phase Response")
xlabel("w"); ylabel("Phase");

21
Output

Figure 13: Solution to a given difference equation

Discussion
During the lab exercise, we analyzed the frequency response of a given
system using the freqz function. The system could be specified in various
forms:
• Transfer Function: We directly used the transfer function
representation.
• Poles and Zeros: We worked with the poles and zeros representation.
• Difference Equation: For systems described by a difference equation,
we manually converted it into an appropriate transfer function form
before plotting the frequency response. This allowed us to visualize
both the magnitude and phase response.
Conclusion
Hence, this lab session was successfully completed by plotting the frequency
response of the given system.

22
Lab 5 - Digital Filters
Objectives
• To learn about the design of Butterworth filter with given specifications.
• To learn about the design of Chebyshev I filter with given specifications.

Butterworth Filter
The Butterworth filter is a type of signal processing filter designed to have a
frequency response that is as flat as possible in the passband. It is also known
as a maximally flat magnitude filter.

Characteristics
• Flat Response in Passband: The Butterworth filter is designed to have no
ripples in the passband. The response is maximally flat, meaning the
magnitude response is smooth and monotonically decreasing without
any ripples.
• Smooth Transition: The transition from the passband to the stopband is
gradual compared to other types of filters like Chebyshev filters.
• Order of the Filter: The order of the Butterworth filter determines the
sharpness of the transition between the passband and the stopband.
Higher-order filters have a steeper roll-off but require more
computational resources.
Transfer Function

The transfer function of an n-th order Butterworth filter is given by:

H(s) = r 1

1 + ωsc2n

where s is the complex frequency variable, ωc is the cutoff frequency, and n is


the order of the filter.

Chebyshev Type I Filter


The Chebyshev Type I filter is a type of signal processing filter that has a
steeper roll-off than the Butterworth filter. This type of filter allows for ripples

23
in the passband to achieve a faster transition between the passband and the
stopband.

Characteristics

• Passband Ripples: The Chebyshev Type I filter has ripples in the passband,
which allows it to achieve a steeper roll-off compared to the Butterworth
filter.

• Sharper Transition: The transition from the passband to the stopband is


sharper, meaning the filter can better distinguish between frequencies in
the passband and stopband.

• Order of the Filter: Similar to the Butterworth filter, the order of the
Chebyshev filter determines the steepness of the roll-off. Higherorder
filters have a steeper transition.

Transfer Function

The transfer function of an n-th order Chebyshev Type I filter is given by:

H(s) = r 1
1 + ϵ2Tn2 ωsc

where s is the complex frequency variable, ωc is the cutoff frequency, ϵ is the


ripple factor, and Tn is the n-th order Chebyshev polynomial.

Programs
Butterworth Filter Design
fp = 1000; fs =
2000; Ap = 1;
As = 40;
Fs = 10000;

Wp = fp / (Fs / 2);
Ws = fs / (Fs / 2);

[n, Wn] = buttord(Wp, Ws, Ap, As); [b, a] =


butter(n, Wn); freqz(b, a, 1024, Fs);

24
utput

Figure 14: Response of the designed Butterworth Filter

Chebyshev I Filter Design


fp = 1000; fs =
2000; Ap = 1;
As = 40;
Fs = 10000;

Wp = fp / (Fs / 2);
Ws = fs / (Fs / 2);

[n, Wn] = cheb1ord(Wp, Ws, Ap, As); [b, a] =


cheby1(n, Ap*3, Wn); freqz(b, a, 1024, Fs);

25
Output

Figure 15: Response of the designed Chebyshev I Filter

Discussion
In this lab exercise, we looked at the design of digital low pass filters (LPF) in
Octave. The filters were designed using Butterworth and Chebyshev I
approximations for the given specifications (passband, stopband). The
frequency response of the designed filter was plotted and verified. The order
required for the filter was also determined using Octave.

Conclusion
Hence, this lab session was successfully completed by designing Butterworth
and Chebyshev I filter for given specifications and observing their frequency
response.

26

You might also like