0% found this document useful (0 votes)
7 views14 pages

Pcs 2

The document contains MATLAB code examples for various signal processing techniques including Ramp, DSBSC, SSB, VSB, FM, PM, ADC, and line coding. Each section provides code for generating, modulating, demodulating, and plotting signals, showcasing different modulation schemes and quantization methods. The code is structured to illustrate the principles of each technique through visual representations and signal transformations.

Uploaded by

iqam bin yunus
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)
7 views14 pages

Pcs 2

The document contains MATLAB code examples for various signal processing techniques including Ramp, DSBSC, SSB, VSB, FM, PM, ADC, and line coding. Each section provides code for generating, modulating, demodulating, and plotting signals, showcasing different modulation schemes and quantization methods. The code is structured to illustrate the principles of each technique through visual representations and signal transformations.

Uploaded by

iqam bin yunus
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/ 14

1)Ramp

% Repeating Ramp Function in MATLAB

% Define time vector


t = linspace(0, 20, 1000); % Time from 0 to 20, smooth resolution

% Define ramp period and slope


T = 5; % Period of the ramp (in seconds)
slope = 1; % Slope of the ramp

% Create repeating ramp waveform using modulus


ramp = slope * mod(t, T);

% Plot the repeating ramp function


figure;
plot(t, ramp, 'r', 'LineWidth', 2);
grid on;
xlabel('Time (t)');
ylabel('Ramp(t)');
title('Repeating (Periodic) Ramp Function');
legend(['Ramp with period T = ' num2str(T)], 'Location', 'northwest');

2) DSBSC
fs = 100000; % Higher sampling frequency for better resolution
t = 0:1/fs:0.05; % Shorter time window to see detail
fm = 100; % Message frequency (Hz)
fc = 2000; % Carrier frequency (Hz)
Am = 1; % Message amplitude
Ac = 1; % Carrier amplitude

% Message Signal
m = Am * sin(2*pi*fm*t);

% Carrier Signal
c = Ac * cos(2*pi*fc*t);

% DSB-SC Modulation
dsbsc = m .* c;

% Coherent Demodulation
demod = dsbsc .* c;

% Design Low-Pass Filter (cutoff just above fm)


cutoff = 2 * fm / fs;
[b, a] = butter(6, cutoff);

% Filter the demodulated signal


recovered = filter(b, a, demod);
% Normalize amplitude to match original message
recovered = recovered * (Am / max(abs(recovered)));

% Plotting
figure;

subplot(4,1,1);
plot(t, m);
title('Original Message Signal');
xlabel('Time (s)'); ylabel('Amplitude');

subplot(4,1,2);
plot(t, dsbsc);
title('DSB-SC Modulated Signal');
xlabel('Time (s)'); ylabel('Amplitude');

subplot(4,1,3);
plot(t, demod);
title('Demodulated Signal (Before LPF)');
xlabel('Time (s)'); ylabel('Amplitude');

subplot(4,1,4);
plot(t, recovered);
title('Recovered Signal (After LPF, Amplitude Normalized)');
xlabel('Time (s)'); ylabel('Amplitude');

2)SSB
% Parameters
fs = 100000; % High sampling frequency
t = 0:1/fs:0.05; % Short time window for clarity
fm = 100; % Message frequency (Hz)
fc = 2000; % Carrier frequency (Hz)
Am = 1; % Message amplitude
Ac = 1; % Carrier amplitude

% Message Signal
m = Am * sin(2*pi*fm*t);

% Generate analytic signal using Hilbert transform


m_analytic = hilbert(m);

% SSB Modulation (USB)


ssb = real(m_analytic .* exp(1j*2*pi*fc*t));

% Coherent Demodulation
carrier = cos(2*pi*fc*t); % Coherent carrier
demod = ssb .* 2 .* carrier; % Multiply by 2*cos(2*pi*fc*t)

% Low-pass filter
cutoff = 2 * fm / fs; % Normalized cutoff frequency
[b, a] = butter(6, cutoff); % 6th-order Butterworth filter
recovered = filter(b, a, demod);
% Normalize recovered signal to match amplitude
recovered = recovered * (Am / max(abs(recovered)));

% Plotting
figure;

subplot(4,1,1);
plot(t, m, 'b');
title('Original Message Signal');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

subplot(4,1,2);
plot(t, ssb, 'r');
title('SSB Modulated Signal (Upper Sideband)');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

subplot(4,1,3);
plot(t, demod, 'g');
title('Demodulated Signal (Before LPF)');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

subplot(4,1,4);
plot(t, recovered, 'k');
title('Recovered Signal (After LPF, Amplitude Normalized)');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

3)VSB
% VSB Modulation and Demodulation Example

fs = 10000; % Sampling frequency


t = 0:1/fs:0.5-1/fs; % Time vector
fm = 100; % Message frequency
fc = 1000; % Carrier frequency

% Message signal (message modulated at fm)


m = cos(2*pi*fm*t);

% Carrier signal (carrier modulated at fc)


c = cos(2*pi*fc*t);

% DSB-SC Modulated signal (simple multiplication of message and carrier)


dsb_sc = m .* c;

% VSB filter (simulated as a low-pass filter — simplified model)


[b, a] = butter(6, 0.2); % Butterworth low-pass filter as vestigial sideband
filter
vsb_signal = filter(b, a, dsb_sc);

% Coherent Demodulation (multiply by the carrier signal again)


demod = vsb_signal .* c; % Multiply again with carrier

% Low-pass filter to recover the baseband signal (message)


recovered = filter(b, a, demod);

% Normalize recovered signal to match the original message amplitude


recovered = recovered * (max(abs(m)) / max(abs(recovered)));

% Plotting the results


figure;

subplot(4,1,1);
plot(t, m, 'b');
title('Original Message Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

subplot(4,1,2);
plot(t, dsb_sc, 'k');
title('DSB-SC Modulated Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

subplot(4,1,3);
plot(t, vsb_signal, 'r');
title('VSB Modulated Signal (Filtered DSB-SC)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

subplot(4,1,4);
plot(t, recovered, 'g');
title('Recovered Message Signal (After Demodulation & LPF)');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

4)FM

% Narrowband and Wideband FM Spectrum Analysis in MATLAB


% Parameters

Fs = 10000; % Sampling frequency (Hz)

t = 0:1/Fs:0.1; % Time vector

fc = 1000; % Carrier frequency (Hz)

kf_narrow = 2*pi*10; % Frequency sensitivity for Narrowband FM (small deviation)

kf_wide = 2*pi*100; % Frequency sensitivity for Wideband FM (large deviation)

% Message signal (sine wave)

fm = 100; % Message frequency (Hz)

m = cos(2*pi*fm*t); % Message signal

% Narrowband FM Modulation (small frequency deviation)

int_m_narrow = cumsum(m) / Fs;

narrow_fm_signal = cos(2*pi*fc*t + kf_narrow * int_m_narrow);

% Wideband FM Modulation (large frequency deviation)

int_m_wide = cumsum(m) / Fs;

wide_fm_signal = cos(2*pi*fc*t + kf_wide * int_m_wide);

% Spectrum analysis using FFT for both NBFM and WBFM

N = length(narrow_fm_signal); % Length of the signal

f_axis = Fs*(0:N-1)/N; % Frequency axis for the spectrum

% Narrowband FM Spectrum

narrow_fm_fft = abs(fft(narrow_fm_signal));

% Wideband FM Spectrum

wide_fm_fft = abs(fft(wide_fm_signal));
% Plotting the spectra

figure;

% Narrowband FM Spectrum Plot

subplot(2,1,1);

plot(f_axis(1:N/2), narrow_fm_fft(1:N/2)); % Plot first half of the spectrum

title('Narrowband FM Spectrum');

xlabel('Frequency (Hz)'); ylabel('Magnitude');

xlim([0 Fs/2]);

% Wideband FM Spectrum Plot

subplot(2,1,2);

plot(f_axis(1:N/2), wide_fm_fft(1:N/2)); % Plot first half of the spectrum

title('Wideband FM Spectrum');

xlabel('Frequency (Hz)'); ylabel('Magnitude');

xlim([0 Fs/2]);

5) PM
% PM with Demodulation in MATLAB

% Parameters
Fs = 10000; % Sampling frequency (Hz)
t = 0:1/Fs:0.1; % Time vector (0 to 0.1s)
fc = 1000; % Carrier frequency (Hz)
kf = pi; % Phase sensitivity

% Message signal (simple sine wave)


fm = 100; % Message frequency (Hz)
m = cos(2*pi*fm*t); % Message signal

% Phase Modulation
pm = cos(2*pi*fc*t + kf * m); % PM signal

% PM Demodulation (Differentiation and Envelope Detection)


x_diff = diff([0 pm]) * Fs; % Differentiate the PM signal
x_env = abs(hilbert(x_diff)); % Envelope detection using Hilbert transform
% Low-pass filter to recover original signal
lpFilt = designfilt('lowpassfir', 'PassbandFrequency', 200, ...
'StopbandFrequency', 300, 'SampleRate', Fs);
demod = filter(lpFilt, x_env);

% Normalize demodulated signal to range -1 to 1


demod_norm = 2 * (demod - min(demod)) / (max(demod) - min(demod)) - 1;

% Plot results
figure;

% Plot original message signal


subplot(4,1,1);
plot(t, m);
title('Message Signal');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

% Plot PM modulated signal


subplot(4,1,2);
plot(t, pm);
title('PM Signal');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

% Plot demodulated signal (before low-pass filter)


subplot(4,1,3);
plot(t(2:end), x_env(2:end)); % Skip first element due to diff
title('Demodulated Signal (Envelope Detection)');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

% Plot demodulated signal (after low-pass filter)


subplot(4,1,4);
plot(t, demod_norm);
title('Recovered Message Signal (After LPF and Normalization)');
xlabel('Time (s)'); ylabel('Amplitude');
grid on;

6) ADC

clc; clear;
% Signal and Sampling
fs = 1000; % Sampling frequency
t = 0:1/fs:0.02; % Time vector
x = 2 * sin(2*pi*50*t); % Original analog signal

% Quantization setup
n_bits = 3; % Number of bits
L = 2^n_bits; % Levels
xmax = max(abs(x)); % Max amplitude
delta = 2 * xmax / L; % Step size

% ---- Mid-Tread Quantization ----


q_midtread = delta * round(x / delta); % Round to nearest step

% Binary Encoding for Mid-Tread


index_midtread = round((q_midtread + xmax) / delta);
binary_midtread = dec2bin(index_midtread, n_bits);

% ---- Mid-Rise Quantization ----


q_midrise = delta * (floor(x / delta) + 0.5); % Mid-rise

% Binary Encoding for Mid-Rise


index_midrise = floor((q_midrise + xmax) / delta);
binary_midrise = dec2bin(index_midrise, n_bits);

% ---- Demodulation / Reconstruction (Zero-order hold) ----


% Demodulation (Reconstruction of the quantized signal)
demod_midtread = q_midtread; % Reconstructed Mid-Tread
demod_midrise = q_midrise; % Reconstructed Mid-Rise

% ---- Normalize the Demodulated Signal to Match the Original Signal ----
demod_midtread = (demod_midtread / max(abs(demod_midtread))) * max(abs(x)); %
Normalize to original amplitude
demod_midrise = (demod_midrise / max(abs(demod_midrise))) * max(abs(x)); %
Normalize to original amplitude

% ---- Plot Results ----


figure;

% Original Signal Plot


subplot(4,1,1);
plot(t, x, 'k');
title('Original Analog Signal');
xlabel('Time');
ylabel('Amplitude');

% Mid-Tread Quantized Signal Plot


subplot(4,1,2);
stairs(t, q_midtread, 'b');
title('Mid-Tread Quantized Signal');
xlabel('Time');
ylabel('Amplitude');

% Mid-Rise Quantized Signal Plot


subplot(4,1,3);
stairs(t, q_midrise, 'r');
title('Mid-Rise Quantized Signal');
xlabel('Time');
ylabel('Amplitude');

% Mid-Tread Demodulated (Reconstructed) Signal Plot


subplot(4,1,4);
stairs(t, demod_midtread, 'g');
title('Mid-Tread Demodulated (Reconstructed) Signal');
xlabel('Time');
ylabel('Amplitude');

% ---- Show Sample Binary Codes ----


disp('First 5 binary codes (Mid-Tread):');
disp(binary_midtread(1:5,:));

disp('First 5 binary codes (Mid-Rise):');


disp(binary_midrise(1:5,:));

7) line code

clc; clear;

% --- USER INPUT ---

binary_str = input('Enter binary sequence (e.g. 1011001): ', 's');

data = binary_str - '0'; % Convert string to numeric array

% --- Parameters ---

bit_duration = 1;

fs = 100; % Samples per bit

t = 0:1/fs:bit_duration - 1/fs;

total_time = [];

% Time vector for full signal

for i = 1:length(data)

total_time = [total_time, t + (i - 1)*bit_duration];

end

% Initialize signals

unipolar_nrz = [];

bipolar_nrz = [];
bipolar_rz = [];

manchester = [];

on_off = [];

last_polarity = -1;

% --- Encoding ---

for i = 1:length(data)

bit = data(i);

% Unipolar NRZ

unipolar_nrz = [unipolar_nrz, bit * ones(1, fs)];

% Bipolar NRZ

if bit == 1

last_polarity = -last_polarity;

bipolar_nrz = [bipolar_nrz, last_polarity * ones(1, fs)];

else

bipolar_nrz = [bipolar_nrz, zeros(1, fs)];

end

% Bipolar RZ

if bit == 1

last_polarity = +last_polarity;

bipolar_rz = [bipolar_rz, last_polarity * ones(1, fs/2), zeros(1, fs/2)];

else

bipolar_rz = [bipolar_rz, zeros(1, fs)];

end
% Manchester

if bit == 1

manchester = [manchester, ones(1, fs/2), -1*ones(1, fs/2)];

else

manchester = [manchester, -1*ones(1, fs/2), ones(1, fs/2)];

end

% On-Off Keying

on_off = [on_off, bit * ones(1, fs)];

end

% --- Plotting ---

figure('Name', 'Line Coding Schemes', 'NumberTitle', 'off');

subplot(5,1,1);

plot(total_time, unipolar_nrz, 'LineWidth', 1.5);

title('Unipolar NRZ'); ylim([-1.5 1.5]); grid on;

subplot(5,1,2);

plot(total_time, bipolar_nrz, 'LineWidth', 1.5);

title('Bipolar NRZ'); ylim([-1.5 1.5]); grid on;

subplot(5,1,3);

plot(total_time, bipolar_rz, 'LineWidth', 1.5);

title('Bipolar RZ'); ylim([-1.5 1.5]); grid on;

subplot(5,1,4);

plot(total_time, manchester, 'LineWidth', 1.5);

title('Manchester'); ylim([-1.5 1.5]); grid on;


subplot(5,1,5);

plot(total_time, on_off, 'LineWidth', 1.5);

title('On-Off Keying'); ylim([-0.5 1.5]); grid on;

xlabel('Time');

% --- Display Input ---

disp(['Binary Input: ', binary_str]);

8) audio signal

clc;

clear;

% Parameters for real-time spectrum analysis

fs = 8000; % Sampling frequency

n_fft = 512; % FFT length (number of frequency bins)

window_size = 256; % Window size (number of samples per frame)

overlap = 128; % Overlap between adjacent frames

n_frames = 100; % Number of frames to process (adjust based on real-time requirement)

% Initialize audio input (microphone)

audio_device = audioread('your_audio_file.wav'); % Replace this with microphone recording function

% Compute Short-Time Fourier Transform (STFT)

figure;

subplot(2,1,1);
specgram(audio_device, n_fft, fs, hamming(window_size), overlap);

title('Real-Time Speech Spectrum');

xlabel('Time (s)');

ylabel('Frequency (Hz)');

% Real-time plotting setup

subplot(2,1,2);

hold on;

xlabel('Time (s)');

ylabel('Amplitude');

axis([0, 100, -1, 1]);

title('Real-Time Speech Signal');

% Main real-time processing loop

for i = 1:n_frames

% Capture audio frame (here you can replace this with live microphone input)

audio_frame = audio_device(i : i + window_size - 1);

% Compute FFT of the frame

fft_frame = fft(audio_frame, n_fft);

freq = (0:n_fft-1) * (fs / n_fft); % Frequency axis

% Plot the spectrum (magnitude)

subplot(2,1,1);

plot(freq, abs(fft_frame(1:n_fft/2))); % Plot the magnitude spectrum

xlim([0, 4000]); % Focus on lower frequencies (for speech)

xlabel('Frequency (Hz)');

ylabel('Magnitude');

title('Real-Time Spectrum Analysis');


% Update time-domain signal plot

subplot(2,1,2);

plot(i * (1 / fs) + (0:window_size-1) / fs, audio_frame, 'k');

drawnow; % Update plots

end

9)Sinc
% Repeating Sinc Function in MATLAB

% Parameters
T = 5; % Period of repetition
numPeriods = 4; % Number of periods
t_single = linspace(-2.5, 2.5, 250); % Time for one sinc pulse

% Create one sinc pulse


sinc_pulse = sinc(t_single); % Built-in MATLAB sinc = sin(pi*x)/(pi*x)

% Generate full time vector for repeated sinc


t = [];
sinc_repeat = [];

% Repeat sinc at intervals of T


for k = 0:numPeriods-1
t = [t, t_single + k*T]; % Shift time for each period
sinc_repeat = [sinc_repeat, sinc_pulse]; % Append pulse
end

% Plot the repeated sinc waveform


figure;
plot(t, sinc_repeat, 'm', 'LineWidth', 2);
grid on;
xlabel('Time (t)');
ylabel('Amplitude');
title('Repeated Sinc Waveform');
legend('Repeated sinc(t)', 'Location', 'northeast');

You might also like