Prac 3
Prac 3
Objective: To learn how to generate and plot different continuous signals in MATLAB and to
perform different operations on signals.
OUTPUT
(b) Unit Step Signal
OUTPUT
a) x(t)=0.1Sin(20*pi*t)+Sin(10*pi*t)+5Cos(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
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
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
% 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
OUTPUT
Q3:
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