0% found this document useful (0 votes)
6 views

all programs

The document provides a comprehensive overview of various signal processing techniques, including the generation and visualization of basic signals such as unit step, rectangular, triangle, sinusoidal, and exponential signals. It also covers advanced topics like amplitude and frequency modulation, sampling and reconstruction of signals, time division multiplexing, PCM illustration, and pulse generation techniques. Each section includes MATLAB code snippets for signal generation and plotting, demonstrating practical applications in signal processing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

all programs

The document provides a comprehensive overview of various signal processing techniques, including the generation and visualization of basic signals such as unit step, rectangular, triangle, sinusoidal, and exponential signals. It also covers advanced topics like amplitude and frequency modulation, sampling and reconstruction of signals, time division multiplexing, PCM illustration, and pulse generation techniques. Each section includes MATLAB code snippets for signal generation and plotting, demonstrating practical applications in signal processing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Basic Signals and Signal Graphing:


a) Unit step
% Define the time range
t = -5:0.01:5;

% Define the unit step signal function


unit_step = @(t) (t >= 0);

% Generate the unit step signal


unit_step_signal = unit_step(t);

% Plotting
plot(t, unit_step_signal, 'b', 'LineWidth', 2);
title('Unit Step Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

b) Rectangular
% Define the time range
t = -5:0.01:5;

% Define the rectangular signal function


rectangular = @(t, width) (abs(t) <= width/2);

% Set the width of the rectangular signal


width_rectangular = 2;

% Generate the rectangular signal


rectangular_signal = rectangular(t, width_rectangular);

% Plotting
plot(t, rectangular_signal, 'r', 'LineWidth', 2);
title('Rectangular Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
c) Standard Triangle
% Define the time range
t = -5:0.01:5;

% Define the standard triangle signal function


standard_triangle = @(t, period) (1 - abs(mod(t, period) - period/2) / (period/2));

% Set the period of the triangle signal


period_triangle = 2;

% Generate the standard triangle signal


standard_triangle_signal = standard_triangle(t, period_triangle);

% Plotting
plot(t, standard_triangle_signal, 'g', 'LineWidth', 2);
title('Standard Triangle Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
d) Sinusoidal
% Define the time range
t = -5:0.01:5;

% Define the parameters for the sinusoidal signal


frequency_sine = 1; % Frequency in Hz
phase_sine = pi/4; % Phase shift in radians

% Generate the sinusoidal signal


sinusoidal_signal = sin(2*pi*frequency_sine*t + phase_sine);

% Plotting
plot(t, sinusoidal_signal, 'm', 'LineWidth', 2);
title('Sinusoidal Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

e) Exponential signal
% Define the time range
t = -5:0.01:5;

% Define the parameters for the exponential signal


alpha_exp = 0.5; % Decay rate

% Generate the exponential signal


exponential_signal = exp(alpha_exp * t);

% Plotting
plot(t, exponential_signal, 'c', 'LineWidth', 2);
title('Exponential Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;
All the a, b, c, d and e programs in one
% Define the time range
t = -5:0.01:5;

% a) Unit Step Signal


unit_step_signal = double(t >= 0);

% b) Rectangular Signal
width_rectangular = 2;
rectangular_signal = double(abs(t) <= width_rectangular/2);

% c) Standard Triangle Signal


period_triangle = 2;
standard_triangle_signal = 1 - abs(mod(t, period_triangle) - period_triangle/2) / (period_triangle/2);

% d) Sinusoidal Signal
frequency_sine = 1;
phase_sine = pi/4;
sinusoidal_signal = sin(2*pi*frequency_sine*t + phase_sine);

% e) Exponential Signal
alpha_exp = 0.5;
exponential_signal = exp(alpha_exp * t);

% Plotting
figure;

subplot(3, 2, 1);
plot(t, unit_step_signal, 'b', 'LineWidth', 2);
title('Unit Step Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

subplot(3, 2, 2);
plot(t, rectangular_signal, 'r', 'LineWidth', 2);
title('Rectangular Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

subplot(3, 2, 3);
plot(t, standard_triangle_signal, 'g', 'LineWidth', 2);
title('Standard Triangle Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

subplot(3, 2, 4);
plot(t, sinusoidal_signal, 'm', 'LineWidth', 2);
title('Sinusoidal Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

subplot(3, 2, 5);
plot(t, exponential_signal, 'c', 'LineWidth', 2);
title('Exponential Signal');
xlabel('Time');
ylabel('Amplitude');
grid on;

% Adjusting layout
sgtitle('Basic Signals and Signal Graphing');
2. Illustration of signal representation in time and frequency domains for a rectangular pulse.
% Define time vector
t = -1:0.01:1;

% Generate rectangular pulse


rect_pulse = rectpuls(t, 0.5); % Rectangular pulse with width 0.5 units

% Compute Fourier Transform


fft_pulse = fftshift(fft(rect_pulse));
frequencies = linspace(-1, 1, length(fft_pulse));

% Plotting
figure;

% Time Domain Plot


subplot(2,1,1);
plot(t, rect_pulse, 'b', 'LineWidth', 2);
title('Rectangular Pulse in Time Domain');
xlabel('Time');
ylabel('Amplitude');
grid on;

% Frequency Domain Plot


subplot(2,1,2);
plot(frequencies, abs(fft_pulse), 'r', 'LineWidth', 2);
title('Rectangular Pulse in Frequency Domain');
xlabel('Frequency');
ylabel('Magnitude');
grid on;
3. Amplitude Modulation and Demodulation: Generation and display the relevant signals and
its spectrum.
% Parameters
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
Fc = 100; % Carrier frequency (Hz)
Am = 0.5; % Modulation depth (amplitude of modulating signal)
Fm = 10; % Modulating frequency (Hz)

% Modulating signal (message signal)


m = Am*sin(pi*Fm*t);

% Carrier signal
c = sin(pi*Fc*t);

% Modulated signal (AM signal)


s = (1 + m).*c;

% Demodulation
s_demod = s .* c; % Demodulation by multiplying with carrier

% Plotting
figure;

% Time Domain Plots


subplot(3, 1, 1);
plot(t, m, 'b', 'LineWidth', 2);
title('Modulating Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

subplot(3, 1, 2);
plot(t, c, 'r', 'LineWidth', 2);
title('Carrier Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

subplot(3, 1, 3);
plot(t, s, 'm', 'LineWidth', 2);
hold on;
plot(t, s_demod, 'g', 'LineWidth', 2);
title('Modulated and Demodulated Signals');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Modulated Signal', 'Demodulated Signal');
grid on;
4. Frequency Modulation and Demodulation: Generation and display the relevant signals and
its spectrum.
% Parameters
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
Fc = 100; % Carrier frequency (Hz)
Fm = 10; % Modulating frequency (Hz)
kf = 50; % Frequency deviation constant (Hz/V)
m = sin(2*pi*Fm*t); % Modulating signal (message signal)

% Frequency Modulation
s_fm = cos(2*pi*Fc*t + kf*cumsum(m));

% Frequency Demodulation
s_demod = diff(s_fm);

% Plotting
figure;

% Modulating Signal Plot


subplot(3, 1, 1);
plot(t, m, 'b', 'LineWidth', 2);
title('Modulating Signal (Message)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Modulated Signal Plot


subplot(3, 1, 2);
plot(t, s_fm, 'm', 'LineWidth', 2);
title('Modulated Signal (FM)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Demodulated Signal Plot


subplot(3, 1, 3);
plot(t(1:end-1), s_demod, 'g', 'LineWidth', 2);
title('Demodulated Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Adjusting layout
sgtitle('Frequency Modulation and Demodulation (Simple Version)');

5. Sampling and reconstruction of low pass signals. Display the signals and its spectrum.
% Parameters
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
Fc = 50; % Cutoff frequency of low-pass filter (Hz)
Am = 0.7; % Amplitude of the signal
Fm = 10; % Frequency of the signal (Hz)

% Original low-pass signal


m_lp = Am * sin(2*pi*Fm*t);

% Fourier Transform of the original signal


M_lp = fft(m_lp);

% Sampling
Fs_new = 400; % New sampling frequency (Hz)
T_new = 1 / Fs_new; % New sampling period
t_new = 0:T_new:t(end); % New time vector
m_sampled = interp1(t, m_lp, t_new, 'linear');

% Reconstruction
m_reconstructed = interp1(t_new, m_sampled, t, 'linear');

% Plotting
figure;

% Original Signal Plot


subplot(3, 1, 1);
plot(t, m_lp, 'b', 'LineWidth', 2);
title('Original Low-Pass Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Sampled Signal Plot


subplot(3, 1, 2);
plot(t_new, m_sampled, 'g', 'LineWidth', 2);
title('Sampled Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Reconstructed Signal Plot


subplot(3, 1, 3);
plot(t, m_reconstructed, 'c', 'LineWidth', 2);
title('Reconstructed Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Adjusting layout
sgtitle('Sampling and Reconstruction of Low Pass Signals');
6. Time Division Multiplexing and Demultiplexing.
Fs = 1000; % Sampling frequency
duration = 10; % Duration of the signals (seconds)

% Generate two example signals


t = 0:1/Fs:duration; % Time vector
signal1 = sin(2*pi*10*t); % Example signal 1 (10 Hz sine wave)
signal2 = square(2*pi*5*t); % Example signal 2 (5 Hz square wave)

% Combine the two signals using TDM


combined_signal = [signal1; signal2]; % Combine signals vertically

% Time Division Demultiplexing (TDD) to extract the original signals


num_signals = size(combined_signal, 1); % Number of signals multiplexed
demux_signal1 = combined_signal(1, :); % Extract signal 1
demux_signal2 = combined_signal(2, :); % Extract signal 2

% Plot the original signals and the multiplexed signal


subplot(num_signals+1, 1, 1);
plot(t, signal1);
title('Original Signal 1');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(num_signals+1, 1, 2);
plot(t, signal2);
title('Original Signal 2');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(num_signals+1, 1, num_signals+1);
plot(t, combined_signal(1, :), 'b', t, combined_signal(2, :), 'r');
title('Multiplexed Signal');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Signal 1', 'Signal 2');
OR
% Define parameters
Fs = 1000; % Sampling frequency
duration = 10; % Duration of the signals (seconds)
num_signals = 2; % Number of signals to multiplex

% Generate example signals


t = 0:1/Fs:duration; % Time vector
signal1 = sin(2*pi*10*t); % Example signal 1 (10 Hz sine wave)
signal2 = square(2*pi*5*t); % Example signal 2 (5 Hz square wave)

% Time Division Multiplexing (TDM)


combined_signal = [signal1; signal2]; % Combine signals vertically

% Time Division Demultiplexing (TDD)


demux_signal1 = combined_signal(1, :); % Extract signal 1
demux_signal2 = combined_signal(2, :); % Extract signal 2

% Plot the original signals, multiplexed signal, and demultiplexed signals


subplot(num_signals+2, 1, 1);
plot(t, signal1);
title('Original Signal 1');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(num_signals+2, 1, 2);
plot(t, signal2);
title('Original Signal 2');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(num_signals+2, 1, num_signals+2);
plot(t, combined_signal(1, :), 'b', t, combined_signal(2, :), 'r');
title('Multiplexed Signal');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Signal 1', 'Signal 2');

% Plot demultiplexed signals


for i = 1:num_signals
subplot(num_signals+2, 1, i+2);
plot(t, combined_signal(i, :), 'b--');
title(['Demultiplexed Signal ', num2str(i)]);
xlabel('Time (s)');
ylabel('Amplitude');
end
7. PCM Illustration: Sampling, Quantization and Encoding.
clc;
clear all;
close all;
f=10;
fs=20*f;
t=0:1/fs:1;
a=2;

x=a*sin(2*pi*f*t);
subplot(3,2,1)
plot(t,x)
title('figure.1 Analog signal');

x1=x+2;
subplot(3,2,2)
plot(t,x1)
title('figure.2 Amplitude Shifted Analog signal');

subplot(3,2,3)
stem(t,x1)
title('figure.3 Sampled Analog signal');

q=round(x1);
subplot(3,2,4)
stem(t,q)
title('figure.4 Quantized signal');

enco=de2bi(q,'left-msb');
subplot(3,2,5)
plot(t,enco)
title('figure.5 PCM encodedsignal');
%pcm decoding
deco=bi2de(enco,'left-msb');
subplot(3,2,6)
plot(t,deco)
title('figure.6 PCM Decoded signal');

8. Generate
a) NRZ, RZ and Raised Cosine pulse
b) Generate and plot eye diagram
% Parameters
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector

% Generate NRZ (Non-Return-to-Zero) pulse


nrz_pulse = square(2*pi*10*t);

% Generate RZ (Return-to-Zero) pulse


rz_pulse = zeros(size(t));
rz_pulse(t < 0.5) = 1;

% Generate Raised Cosine Pulse


alpha = 0.5; % Roll-off factor
rc_pulse_length = length(t);% Length of the Raised Cosine Pulse
rc_pulse = rcosdesign(alpha, 20, rc_pulse_length, 'sqrt');

% Plot the pulses


figure;
subplot(3,1,1);
plot(t, nrz_pulse, 'LineWidth', 2);
title('NRZ (Non-Return-to-Zero) Pulse');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

subplot(3,1,2);
plot(t, rz_pulse, 'LineWidth', 2);
title('RZ (Return-to-Zero) Pulse');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Generate a time vector matching the length of rc_pulse


t_rc = linspace(0, (length(rc_pulse)-1)*T, length(rc_pulse));

subplot(3,1,3);
plot(t_rc, rc_pulse, 'LineWidth', 2);
title('Raised Cosine Pulse');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Generate and plot eye diagrams


eye_diagram(nrz_pulse, Fs, 10); % NRZ pulse
eye_diagram(rz_pulse, Fs, 10); % RZ pulse
eye_diagram(rc_pulse, Fs, 10); % Raised Cosine pulse

function eye_diagram(signal, Fs, oversampling_factor)


% Calculate symbol length
symbol_length = round(Fs / oversampling_factor);

% Calculate number of symbols and remaining samples


num_symbols = floor(length(signal) / symbol_length);
remaining_samples = rem(length(signal), symbol_length);

% Plot the eye diagram


figure;
hold on;
for i = 1:num_symbols
start_index = (i - 1) * symbol_length + 1;
end_index = i * symbol_length;
plot(signal(start_index:end_index));
end

% Plot the remaining samples


if remaining_samples > 0
plot(signal(end - remaining_samples + 1:end), 'Color', [0.5 0.5 0.5]);
end

hold off;

% Set axis labels and title


xlabel('Sample Index');
ylabel('Amplitude');
title('Eye Diagram');
grid on;
end
9. Generate the probability density function of Gaussian distribution Function.
% Parameters
mu = 0; % Mean of the Gaussian distribution
sigma = 1; % Standard deviation of the Gaussian distribution

% Generate x values
x = linspace(-5, 5, 1000); % Range of x values

% Compute the PDF


pdf = (1 / (sigma * sqrt(2 * pi))) * exp(-(x - mu).^2 / (2 * sigma^2));

% Plot the PDF


figure;
plot(x, pdf, 'LineWidth', 2);
xlabel('x');
ylabel('PDF(x)');
title('Probability Density Function of Gaussian Distribution');
grid on;
10. Display the signal and its spectrum of an audio signal.
Fs = 1000; % sampling frequency (Hz), reduced for demonstration
duration = 20; % duration of the signal (seconds), increased for demonstration
frequency = 100; % frequency of the sine wave (Hz)
num_cycles = 5; % number of cycles of the sine wave within the duration

% Time vector for a single cycle


t_single_cycle = 0:1/Fs:(1/frequency);

% Generate the sine wave signal for a single cycle


y_single_cycle = sin(2*pi*frequency*t_single_cycle);

% Repeat the single cycle to create the split signal


y = repmat(y_single_cycle, 1, num_cycles);
t = (0:length(y)-1)/Fs;

% Display the signal waveform


subplot(2,1,1);
plot(t, y);
title('Sine Wave Signal with Split');
xlabel('Time (s)');
ylabel('Amplitude');

% Compute and display the spectrum


Y = fft(y); % compute Fourier transform
N = length(Y); % length of the signal
f = (0:N-1)*(Fs/N); % frequency range
Y = abs(Y)/N; % take the absolute value and normalize
subplot(2,1,2);
plot(f, Y);
title('Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
xlim([0, Fs/2]); % plot only positive frequencies
OR
% Generate a simple sine wave signal
Fs = 44100; % sampling frequency (Hz)
duration = 3; % duration of the signal (seconds)
frequency = 1000; % frequency of the sine wave (Hz)
t = 0:1/Fs:duration; % time vector
y = sin(2*pi*frequency*t); % generate the sine wave signal

% Display the signal waveform


subplot(2,1,1);
plot(t, y);
title('Sine Wave Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Compute and display the spectrum


Y = fft(y); % compute Fourier transform
N = length(Y); % length of the signal
f = (0:N-1)*(Fs/N); % frequency range
Y = abs(Y)/N; % take the absolute value and normalize
subplot(2,1,2);
plot(f, Y);
title('Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
xlim([0, Fs/2]); % plot only positive frequencies

You might also like