DSP LAB
1 LAB.SINE wave program
clear all;
close all;
x = linspace(0, 2*pi, 1000);
y = sin(x);
plot(x, y);
title('Sine Wave');
xlabel('Angle (radians)');
ylabel('sin(x)');
grid on;
2 LAB.Unit Impulse signal
clear all;
close all;
t = -10:10;
impulse_signal = zeros(size(t));
for i = 1:length(t)
if t(i) == 0
impulse_signal(i) = 1;
end
end
stem(t, impulse_signal);
title('Unit Impulse Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
//EXPLANATION -> This MATLAB code generates a unit impulse signal and plots it using the stem function. Here's an
1. **Initialization**:
- `clear all; close all;` are used to clear any existing variables and figures in the MATLAB workspace.
2. **Time Index**:
- `t = -10:10;` defines a time range from -10 to 10. This range determines the duration of the signal.
3. **Unit Impulse Signal Generation**:
- `impulse_signal = zeros(size(t));` initializes an array `impulse_signal` with the same size as `t`, filled with zeros.
- The `for` loop iterates over each element of `t`.
- If the current element `t(i)` is equal to 0 (indicating the current time instant is at the origin), the corresponding e
it impulse at that instant.
4. **Plotting**:
- `stem(t, impulse_signal);` plots the unit impulse signal.
- `title('Unit Impulse Signal');` sets the title of the plot.
- `xlabel('Time'); ylabel('Amplitude');` sets the labels for the x-axis and y-axis, respectively.
- `grid on;` displays the grid on the plot for better readability.
5. **Result**:
- The plot shows a unit impulse signal with an amplitude of 1 at the time instant t=0 and 0 elsewhere. The stem fun
###3 LAB. Unit Step Signal
clear all;
close all;
t = -10:10;
step_signal = zeros(size(t));
for i = 1:length(t)
if t(i) >= 0
step_signal(i) = 1;
end
end
stem(t, step_signal);
title('Unit Step Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
//EXPLANATION
This MATLAB code generates a unit step signal and plots it using the stem function. Here's an explanation of the cod
1. **Initialization**:
- `clear all; close all;` are used to clear any existing variables and figures in the MATLAB workspace.
2. **Time Index**:
- `t = -10:10;` defines a time range from -10 to 10. This range determines the duration of the signal.
3. **Unit Step Signal Generation**:
- `step_signal = zeros(size(t));` initializes an array `step_signal` with the same size as `t`, filled with zeros.
- The `for` loop iterates over each element of `t`.
- If the current element `t(i)` is greater than or equal to 0 (indicating the current time instant is at or after the orig
to 1, creating the unit step signal.
4. **Plotting**:
- `stem(t, step_signal);` plots the unit step signal.
- `title('Unit Step Signal');` sets the title of the plot.
- `xlabel('Time'); ylabel('Amplitude');` sets the labels for the x-axis and y-axis, respectively.
- `grid on;` displays the grid on the plot for better readability.
5. **Result**:
- The plot shows a unit step signal with an amplitude of 1 for times greater than or equal to 0, and 0 for times less t
nature of the signal.
####4 LAB .growing exponential signal without using inbuilt libraryof exp function in digtal signal processing lab ma
% Parameters
A = 1; % Amplitude
alpha = 0.1; % Growth rate
N = 100; % Number of samples
n = 0:N-1; % Time index
% Generate the signal without using exp function
x = zeros(1, N);
for i = 1:N
x(i) = A * (1 + alpha)^i;
end
% Plot the signal
stem(n, x);
xlabel('n');
ylabel('Amplitude');
title('Growing Exponential Signal (Approximated)');
//EXPLANATION
Certainly! Here's an explanation of the code:
1. **Parameters**:
- `A`: Amplitude of the exponential signal.
- `alpha`: Growth rate of the exponential signal.
- `N`: Number of samples to generate.
- `n`: Time index from 0 to `N-1`.
2. **Initialization**:
- We initialize an array `x` of size `N` to store the values of the exponential signal.
3. **Signal Generation**:
- We use a `for` loop to iterate from 1 to `N`.
- For each iteration `i`, we calculate the value of the exponential signal at index `i` using the formula \( A \times (1
4. **Plotting**:
- We use the `stem` function to plot the generated signal `x` against the time index `n`.
- The x-axis represents the time index `n`, and the y-axis represents the amplitude of the signal.
- The title of the plot is "Growing Exponential Signal (Approximated)".
###5 LAB. Plot a decaying exponential signal without using inbuilt library of exp function in digtal signal processing l
% Parameters
A = 1; % Amplitude
alpha = -0.1; % Decay rate (negative for decay)
N = 100; % Number of samples
n = 0:N-1; % Time index
% Generate the signal without using exp function
x = zeros(1, N);
for i = 1:N
x(i) = A * (1 + alpha)^i;
end
% Plot the signal
stem(n, x);
xlabel('n');
ylabel('Amplitude');
title('Decaying Exponential Signal (Approximated)');
//EXPLANATION
Certainly! Here's an explanation of the code:
1. **Parameters**:
- `A`: Amplitude of the decaying exponential signal.
- `alpha`: Decay rate of the exponential signal (negative for decay).
- `N`: Number of samples to generate.
- `n`: Time index from 0 to `N-1`.
2. **Initialization**:
- We clear any existing variables and close any open figures using `clear all; close all;`.
3. **Signal Generation**:
- We initialize an array `x` of size `N` to store the values of the decaying exponential signal.
- The `for` loop iterates over each element of `n` (from 1 to `N`).
- For each iteration `i`, we calculate the value of the decaying exponential signal at index `i` using the formula \(
4. **Plotting**:
- We use the `stem` function to plot the decaying exponential signal `x` against the time index `n`.
- The x-axis represents the time index `n`, and the y-axis represents the amplitude of the signal.
- We set the title of the plot to "Decaying Exponential Signal (Approximated)" using `title('Decaying Exponential Sign
- We set the labels for the x-axis and y-axis using `xlabel('n'); ylabel('Amplitude');`.
###6 LAB.PLOT THE CIRCULAR CONVOLUTION FOR THE TWO SEQUENCES WITH FOUR VALUES OF INPUT IN MATLAB
% Define the two sequences with four values each
x = [1, 2, 3, 4]; % Sequence 1
h = [0.5, 0.2, 0.1, 0.3]; % Sequence 2
% Calculate the circular convolution using cconv
y = cconv(x, h, 4);
% Plot the circular convolution result
stem(0:3, y);
xlabel('Index');
ylabel('Amplitude');
title('Circular Convolution of Two Sequences');
grid on;
//EXPLANATION
Here's an explanation of the code for calculating and plotting the circular convolution of two sequences with four val
1. **Input Sequences**:
- `x = [1, 2, 3, 4];` and `h = [0.5, 0.2, 0.1, 0.3];` define two sequences with four values each. These are the sequence
2. **Circular Convolution Calculation**:
- `y = cconv(x, h, 4);` calculates the circular convolution of sequences `x` and `h` with a result length of 4. The `cc
ircular shift property of the Discrete Fourier Transform (DFT).
3. **Plotting**:
- `stem(0:3, y);` plots the circular convolution result `y` against the index (0 to 3). The `stem` function is used to c
values.
4. **Labels and Title**:
- `xlabel('Index'); ylabel('Amplitude');` set the labels for the x-axis and y-axis, respectively, indicating that the x-axis
plitude of the convolution result.
- `title('Circular Convolution of Two Sequences');` sets the title of the plot to "Circular Convolution of Two Sequence
5. **Grid**:
- `grid on;` adds a grid to the plot for better readability, making it easier to see the values of the convolution result
####7.PLOT THE DISCRETE FOURIER TRANSFORM FOR A FOUR POINT SEQUENCE ON A MATLAB CODE
% Define the four-point sequence
x = [1, 2, 3, 4];
% Calculate the Discrete Fourier Transform (DFT)
X = fft(x);
% Plot the magnitude of the DFT
stem(0:3, abs(X));
xlabel('Frequency (k)');
ylabel('Magnitude');
title('Magnitude of DFT of a Four-Point Sequence');
grid on;
//EXPLANATION
Here's an explanation of the code for calculating and plotting the Discrete Fourier Transform (DFT) of a four-point se
1. **Input Sequence**:
- `x = [1, 2, 3, 4];` defines a four-point sequence for which we want to calculate the DFT.
2. **DFT Calculation**:
- `X = fft(x);` calculates the DFT of the input sequence `x` using the Fast Fourier Transform (FFT) algorithm. The res
3. **Magnitude Plotting**:
- `stem(0:3, abs(X));` plots the magnitude of the DFT coefficients. The `abs` function is used to calculate the magn
- The x-axis represents the frequency index `k`, which ranges from 0 to 3 for a four-point DFT.
- The y-axis represents the magnitude of the DFT coefficients.
4. **Labels and Title**:
- `xlabel('Frequency (k)'); ylabel('Magnitude');` set the labels for the x-axis and y-axis, respectively, indicating that th
represents the magnitude of the DFT coefficients.
- `title('Magnitude of DFT of a Four-Point Sequence');` sets the title of the plot to describe the content.
5. **Grid**:
- `grid on;` adds a grid to the plot for better readability, making it easier to see the values of the DFT coefficients.