0% found this document useful (0 votes)
11 views22 pages

Prac 3

The document outlines an experiment in MATLAB to generate and plot various continuous signals, including unit impulse, step, ramp, parabolic, sine, triangular, trapezoidal, and more. It also includes operations on signals such as modulation, time-reversal, and scaling. Each section provides MATLAB code snippets for generating the signals and visualizing them with appropriate labels and titles.

Uploaded by

xcom1042
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)
11 views22 pages

Prac 3

The document outlines an experiment in MATLAB to generate and plot various continuous signals, including unit impulse, step, ramp, parabolic, sine, triangular, trapezoidal, and more. It also includes operations on signals such as modulation, time-reversal, and scaling. Each section provides MATLAB code snippets for generating the signals and visualizing them with appropriate labels and titles.

Uploaded by

xcom1042
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/ 22

Experiment-3

Objective: To learn how to generate and plot different continuous signals in MATLAB and to
perform different operations on signals.

Q1: Generate and plot different continuous signals in MATLAB

(a) Unit Impulse signal

n = -10:10; % Time index from -10 to 10


x = (n == 0); % Unit impulse: 1 at n = 0, else 0
stem(n, x, 'filled'); % Discrete-time plot
xlabel('n');
ylabel('Amplitude');
title('Unit Impulse Signal');
axis([-10 10 -0.2 1.2]);
grid on;

OUTPUT
(b) Unit Step Signal

n = -10:10; % Time index from -10 to 10


x = double(n >= 0); % Unit step signal: 1 for n >= 0, else 0
stem(n, x, 'filled'); % Discrete-time plot
xlabel('n');
ylabel('Amplitude');
title('Unit Step Signal');
axis([-10 10 -0.2 1.2]);
grid on;

OUTPUT

(c) Unit Ramp Signal

t = -5:0.01:5; % Time vector from -5 to 5


x = t .* (t >= 0); % Unit ramp signal: ramp starts at t=0
figure;
plot(t, x, 'LineWidth', 2);
title('Unit Ramp Signal');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
axis([-5 5 -1 5]);
OUTPUT

(d) Unit Parabolic Signal

t = -5:0.01:5; % Time vector from -5 to 5


x = t.^2 .* (t >= 0); % Unit parabolic signal: parabola starts at t=0
figure;
plot(t, x, 'LineWidth', 2);
title('Unit Parabolic Signal');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
axis([-5 5 0 25]);
OUTPUT

(e) Sine wave with frequency 10Hz and amplitude 2

fs = 1000; % Sampling frequency (Hz)


t = 0:1/fs:1; % Time vector from 0 to 1 second
f = 10; % Frequency in Hz
A = 2; % Amplitude
x = A * sin(2 * pi * f * t);
figure;
plot(t, x, 'LineWidth', 2);
title('Sine Wave with Frequency 10 Hz and Amplitude 2');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
OUTPUT

(f) Gate/ Window signal

x = -3:0.01:3; % Define x range


y = double(abs(x) < 2); % Rectangular pulse from -2 to 2
plot(x, y, 'LineWidth', 1); % Plot with line width for clarity
axis([-3 3 -2 2]); % Set axis limits

(g) Signum Signal

x = -2:0.01:2; % Define x range


y = sign(x); % Signum function
plot(x, y, 'LineWidth', 1);
axis([-2 2 -3 3]); % Set axis limits similar to image
(h) Triangular signal

x = -3:0.01:3; % Define x range


y = 1 - abs(x)/3; % Triangular function: peak at x=0, zero at x=±3
y(y < 0) = 0; % Set negative values to 0 to keep triangle shape
plot(x, y, 'LineWidth', 1); % Plot with line width
axis([-3 3 -2 2]); % Set axis limits

(i) Trapezoidal signal

x = -4:0.01:4; % Define x range


y = zeros(size(x)); % Initialize y with zeros
idx1 = (x >= -4) & (x < -2);
y(idx1) = (x(idx1) + 4) / 2;
idx2 = (x >= -2) & (x <= 2);
y(idx2) = 1;
% Falling edge
idx3 = (x > 2) & (x <= 4);
y(idx3) = (4 - x(idx3)) / 2;
plot(x, y, 'LineWidth', 1);
axis([-4 4 0 1.2]); % Set axis limits

(j) Signal x(t)

x = [-2 0 2 2 4 5 7]; % x-coordinates of the piecewise points


y = [0 1 1 2 2 1 0]; % y-values at each x
plot(x, y, 'LineWidth', 1);
axis([-2 7 0 2]); % Set axis limits
(k) Square wave

x = [-3 -2 -2 0 0 2 2 4 4 6]; % x-coordinates of transitions


y = [1 1 -1 -1 1 1 -1 -1 1 1]; % y-values at each x
plot(x, y, 'LineWidth', 1);
axis([-3 6 -2 2]); % Set axis limits

(l) Sawtooth wave

t = -5:0.01:15; % Time vector


y = -5 - 5 * sawtooth(2 * pi * t / 5); % Inverted sawtooth, period = 5, range [-10, -5]
plot(t, y, 'LineWidth', 1);
axis([-5 15 -10.5 -4.5]); % Set axis limits
Q2: Perform following operations on signals and plot the resulting signals in
MATLAB. Consider suitable time range for each signal

a) x(t)=0.1Sin(20*pi*t)+Sin(10*pi*t)+5Cos(2*pi*t)

fs = 1000; % Sampling frequency


t = 0:1/fs:1; % Time from 0 to 1 second
x = 0.1 * sin(20 * pi * t) + sin(10 * pi * t) + 5 * cos(2 * pi * t);

figure;
plot(t, x, 'LineWidth', 2);
title('Signal: x(t) = 0.1sin(20πt) + sin(10πt) + 5cos(2πt)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT
b) x(t)= 5sin(40*pi*t)+ sin(2*pi*t)

% Time vector
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time vector from 0 to 1 second
x = 5 * sin(40 * pi * t) + sin(2 * pi * t);
figure;
plot(t, x, 'LineWidth', 2);
title('Signal: x(t) = 5sin(40πt) + sin(2πt)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT

c) x(t)= (3+cos(2*pi*t))sin(20*pi*t) (also show the envelopes in dotted line)

fs = 1000; % Sampling frequency


t = 0:1/fs:2; % Time from 0 to 2 seconds for better envelope visibility
% Define the modulated signal
envelope = 3 + cos(2 * pi * t);
x = envelope .* sin(20 * pi * t);
% Plot the signal
figure;
plot(t, x, 'b', 'LineWidth', 1.5); hold on;
plot(t, envelope, 'r--', 'LineWidth', 1.5); % Upper envelope
plot(t, -envelope, 'r--', 'LineWidth', 1.5); % Lower envelope
title('Amplitude Modulated Signal with Envelopes');
xlabel('Time (t)');
ylabel('Amplitude');
legend('x(t)', 'Upper Envelope', 'Lower Envelope');
grid on;

OUTPUT

d) x(t)= Sin(10*pi*t)*[u(t)-u(t-1)]

% Time vector
fs = 1000; % Sampling frequency
t = -0.5:1/fs:1.5; % Time from -0.5 to 1.5 seconds

% Unit step functions


u_t = double(t >= 0); % u(t)
u_t_1 = double(t >= 1); % u(t - 1)
% Window function
window = u_t - u_t_1; % [u(t) - u(t-1)]
% Signal definition
x = sin(10 * pi * t) .* window;
% Plot the signal
figure;
plot(t, x, 'b', 'LineWidth', 2);
title('x(t) = sin(10πt) · [u(t) - u(t - 1)]');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
axis([-0.5 1.5 -1.2 1.2]);

OUTPUT

e) x(t)=e^(-2|t|) Cos(10*pi*t)

% Time vector
fs = 1000; % Sampling frequency
t = -2:1/fs:2; % Time from -2 to 2 seconds
% Signal definition
x = exp(-2 * abs(t)) .* cos(10 * pi * t);
% Plot the signal
figure;
plot(t, x, 'b', 'LineWidth', 2); hold on;
% Plot the exponential envelopes
envelope = exp(-2 * abs(t));
plot(t, envelope, 'r--', 'LineWidth', 1.5); % Upper envelope
plot(t, -envelope, 'r--', 'LineWidth', 1.5); % Lower envelope
% Labels and title
title('x(t) = e^{-2|t|} \cdot cos(10\pi t)');
xlabel('Time (t)');
ylabel('Amplitude');
legend('x(t)', 'Upper Envelope', 'Lower Envelope');
grid on;

OUTPUT

f) x(t)=Sin(pi*t)/(pi*t) (Sinc Function)

% Time vector
t = -10:0.01:10; % Time from -10 to 10
% Define the sinc function
x = sin(pi * t) ./ (pi * t);
% Handle the singularity at t = 0 using L'Hôpital's Rule
x(t == 0) = 1;
% Plot the sinc function
figure;
plot(t, x, 'b', 'LineWidth', 2);
title('x(t) = sinc(t) = sin(\pi t)/(\pi t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
axis([-10 10 -0.3 1.1]);
OUTPUT

g) x(t)=t*Sin(20*pi*t)*u(t)

% Time vector
fs = 1000; % Sampling frequency
t = -1:1/fs:1; % Time from -1 to 1 seconds
% Unit step function
u = double(t >= 0); % u(t)
% Define the signal
x = t .* sin(20 * pi * t) .* u;
% Plot the signal
figure;
plot(t, x, 'b', 'LineWidth', 2);
title('x(t) = t \cdot sin(20\pi t) \cdot u(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
axis([-1 1 -0.5 0.5]);

OUTPUT
h) x(t)=t*e^(-3t) Cos(10*pi*t) u(t)

% Time vector
fs = 1000; % Sampling frequency
t = -1:1/fs:2; % Time from -1 to 2 seconds

% Unit step function


u = double(t >= 0); % u(t)

% Define the signal


x = t .* exp(-3 * t) .* cos(10 * pi * t) .* u;

% Plot the signal


figure;
plot(t, x, 'b', 'LineWidth', 2);
title('x(t) = t \cdot e^{-3t} \cdot cos(10\pi t) \cdot u(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
axis([-1 2 -0.5 0.5]);

OUTPUT
Q3:

a) Generate the signal x(t) as shown below:

x = [-2 0 0 2 2 4 4 5]; % x-coordinates at each transition


y = [1 1 2 2 0 0 0 1]; % corresponding y-values

plot(x, y, 'LineWidth', 1.5);


axis([-2 5 0 2]); % set axis limits
grid on;
b) Plot the following signals:

i) x(-t)

% Time vector
fs = 1000; % Sampling frequency
t = -2:1/fs:2; % Time from -2 to 2 seconds
% Define x(t) (example signal)
x_t = sin(2 * pi * t) .* exp(-abs(t)); % You can change this
% Compute x(-t)
x_neg_t = sin(2 * pi * (-t)) .* exp(-abs(-t)); % or simply: x_neg_t = x_t(end:-1:1);
% Plot both for comparison
figure;
subplot(2,1,1);
plot(t, x_t, 'b', 'LineWidth', 2);
title('Original Signal: x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, x_neg_t, 'r', 'LineWidth', 2);
title('Time-Reversed Signal: x(-t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT
ii) -x(t)

% Time vector
fs = 1000; % Sampling frequency
t = -2:1/fs:2; % Time from -2 to 2 seconds
% Define x(t) (example)
x_t = sin(2 * pi * t) .* exp(-abs(t)); % You can change this to any signal
% Compute -x(t)
neg_x_t = -x_t;
% Plot both x(t) and -x(t)
figure;
subplot(2,1,1);
plot(t, x_t, 'b', 'LineWidth', 2);
title('Original Signal: x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, neg_x_t, 'r', 'LineWidth', 2);
title('Inverted Signal: -x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT
iii) x(2t)

% Time vector
fs = 1000; % Sampling frequency
t = -2:1/fs:2; % Time from -2 to 2 seconds
% Define x(t) (example signal)
x_t = sin(2 * pi * t) .* exp(-abs(t)); % Replace this with your own signal
% Define x(2t) — time-compressed version
x_2t = sin(2 * pi * 2 * t) .* exp(-abs(2 * t)); % or x_2t = x(t*2) if you have a function handle
% Plot both x(t) and x(2t)
figure;
subplot(2,1,1);
plot(t, x_t, 'b', 'LineWidth', 2);
title('Original Signal: x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, x_2t, 'r', 'LineWidth', 2);
title('Time-Scaled Signal: x(2t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT
iv) x(t/4)

% Time vector
fs = 1000; % Sampling frequency
t = -8:1/fs:8; % Time from -8 to 8 seconds
% Define x(t) (example signal)
x_t = sin(2 * pi * t) .* exp(-abs(t)); % Replace this with your desired signal
% Define x(t/4) — time-expanded version
x_t4 = sin(2 * pi * (t / 4)) .* exp(-abs(t / 4));
% Plot both x(t) and x(t/4)
figure;
subplot(2,1,1);
plot(t, x_t, 'b', 'LineWidth', 2);
title('Original Signal: x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, x_t4, 'r', 'LineWidth', 2);
title('Time-Expanded Signal: x(t/4)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT
v) x(t+2)

% Time vector
fs = 1000; % Sampling frequency
t = -5:1/fs:5; % Time from -5 to 5 seconds
% Define x(t) (example signal)
x_t = sin(2 * pi * t) .* exp(-abs(t)); % Replace with your own signal
% Define x(t + 2) — shifted to the left
x_shifted = sin(2 * pi * (t + 2)) .* exp(-abs(t + 2));
% Plot both x(t) and x(t + 2)
figure;
subplot(2,1,1);
plot(t, x_t, 'b', 'LineWidth', 2);
title('Original Signal: x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, x_shifted, 'r', 'LineWidth', 2);
title('Time-Shifted Signal: x(t + 2)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT
vi) x(2t+3)

% Time vector
fs = 1000; % Sampling frequency
t = -5:1/fs:5; % Time from -5 to 5 seconds
% Define x(t) (example signal)
x_t = sin(2 * pi * t) .* exp(-abs(t)); % Replace this with your desired signal
% Define x(2t + 3)
x_scaled_shifted = sin(2 * pi * (2 * t + 3)) .* exp(-abs(2 * t + 3));
% Plot both x(t) and x(2t + 3)
figure;
subplot(2,1,1);
plot(t, x_t, 'b', 'LineWidth', 2);
title('Original Signal: x(t)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;
subplot(2,1,2);
plot(t, x_scaled_shifted, 'r', 'LineWidth', 2);
title('Transformed Signal: x(2t + 3)');
xlabel('Time (t)');
ylabel('Amplitude');
grid on;

OUTPUT

You might also like