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

يغل

The document outlines a MATLAB script for processing an audio signal using Chebyshev Type I filtering, quantization, and Amplitude Shift Keying (ASK) modulation. It includes steps for filtering, quantizing the signal into binary levels, modulating it, adding noise, and demodulating to retrieve the original signal. Finally, it calculates the Bit Error Rate (BER) to evaluate the performance of the transmission.

Uploaded by

bofa2245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

يغل

The document outlines a MATLAB script for processing an audio signal using Chebyshev Type I filtering, quantization, and Amplitude Shift Keying (ASK) modulation. It includes steps for filtering, quantizing the signal into binary levels, modulating it, adding noise, and demodulating to retrieve the original signal. Finally, it calculates the Bit Error Rate (BER) to evaluate the performance of the transmission.

Uploaded by

bofa2245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

clc;

clear;

% Load Signal
load handel.mat;
n = length(y);
fc1_bpf = 500;
fc2_bpf = 3000;
t = (0:n-1)/Fs;
f = (-n/2:n/2-1)*(Fs/n);

% Filter Design Using Chebyshev Type I


[b1, a1] = cheby1(10, 0.5, [fc1_bpf fc2_bpf]/(Fs/2),
'bandpass');

% Filter Application
y_filt = filter(b1, a1, y);

% FFT of the Original Signal


y_fft = fft(y);
y_fft = fftshift(y_fft);

% FFT of the Filtered Signal


y_filt_fft = fft(y_filt);
y_filt_fft = fftshift(y_filt_fft);

% Quantization with m = 4 Levels


m = 4; % Number of quantization levels
y_min = min(y_filt);
y_max = max(y_filt);
quant_levels = linspace(y_min, y_max, m); % Define
quantization levels
y_quantized = interp1(quant_levels, quant_levels, y_filt,
'nearest'); % Nearest neighbor quantization

% Map Quantized Levels to Binary


binary_map = {
[0 0], % Level 1
[0 1], % Level 2
[1 0], % Level 3
[1 1] % Level 4
};
quantized_binary = zeros(length(y_quantized), 2); % Array to
store binary values
for i = 1:m
quantized_binary(y_quantized == quant_levels(i), :) =
repmat(binary_map{i}, sum(y_quantized == quant_levels(i)),
1);
end

% Flatten Binary Array into a Stream of Zeros and Ones


binary_stream = reshape(quantized_binary', [], 1); % Column
vector of zeros and ones

% ASK Modulation Parameters


fc = 20000; % Carrier frequency set to 20000 Hz
Tb = 1e-3; % Bit duration (s)
Fs_mod = 10 * fc; % Sampling frequency for modulation
samples_per_bit = round(Fs_mod * Tb); % Number of samples
per bit
t_mod = (0:samples_per_bit-1) / Fs_mod; % Time vector for
one bit

% Generate Carrier Signals for '1' and '0'


carrier_one = cos(2 * pi * fc * t_mod); % High amplitude
for '1'
carrier_zero = 0.5 * cos(2 * pi * fc * t_mod); % Low
amplitude for '0'

% Preallocate ASK Signal


ask_signal = zeros(1, length(binary_stream) *
samples_per_bit);

% Generate ASK-Modulated Signal Using Vectorization


for i = 1:length(binary_stream)
idx = (i-1)*samples_per_bit + (1:samples_per_bit); %
Index for current bit
if binary_stream(i) == 1
ask_signal(idx) = carrier_one; % Assign high
amplitude carrier for '1'
else
ask_signal(idx) = carrier_zero; % Assign low
amplitude carrier for '0'
end
end
% Time Vector for ASK Signal
t_total = (0:length(ask_signal)-1) / Fs_mod;

% Add Noise to ASK Signal


SNR = 20; % Signal-to-Noise Ratio set to 20 dB
noisy_signal = awgn(ask_signal, SNR, 'measured');

% Demodulation (Envelope Detection)


rectified_signal = abs(hilbert(noisy_signal)); % Envelope
detection
threshold = (max(rectified_signal) +
min(rectified_signal)) / 2; % Threshold
demodulated_bits = rectified_signal > threshold; % Compare
with threshold

% Reshape Demodulated Stream to Binary Groups


demodulated_binary =
reshape(demodulated_bits(1:samples_per_bit:end), 2, [])'; %
Group into 2-bit binary

% Decode Binary to Quantized Levels


decoded_signal = zeros(size(demodulated_binary, 1), 1); %
Preallocate for decoded signal
for i = 1:m
decoded_signal(ismember(demodulated_binary,
binary_map{i}, 'rows')) = quant_levels(i);
end

% Dequantization (Mapping back to original continuous


values)
dequantized_signal = interp1(quant_levels, quant_levels,
decoded_signal, 'linear');

% Plot the Original Signal (Time and Frequency)


figure;
subplot(2, 1, 1);
plot(t, y);
title('Original Signal (Time Domain)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
plot(f, abs(y_fft));
title('Original Signal (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Plot the Filtered Signal (Time and Frequency)


figure;
subplot(2, 1, 1);
plot(t, y_filt);
title('Filtered Signal (Time Domain)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
plot(f, abs(y_filt_fft));
title('Filtered Signal (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Plot the Quantized Signal (Time and Frequency)


figure;
subplot(2, 1, 1);
stairs(t, y_quantized);
title('Quantized Signal (Time Domain)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
quantized_fft = fft(y_quantized);
quantized_fft = fftshift(quantized_fft);
f_quantized =
(-length(quantized_fft)/2:length(quantized_fft)/2-1)*(Fs/len
gth(quantized_fft)); % Frequency vector for quantized signal
plot(f_quantized, abs(quantized_fft));
title('Quantized Signal (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Plot the ASK Signal (Time and Frequency)


figure;
subplot(2, 1, 1);
plot(t_total, ask_signal);
title('ASK Signal (Time Domain)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
ask_signal_fft = fft(ask_signal);
ask_signal_fft = fftshift(ask_signal_fft);
f_ask_signal =
(-length(ask_signal_fft)/2:length(ask_signal_fft)/2-
1)*(Fs_mod/length(ask_signal_fft)); % Frequency vector for
ASK signal
plot(f_ask_signal, abs(ask_signal_fft));
title('ASK Signal (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Plot the Noisy ASK Signal (Time and Frequency)


figure;
subplot(2, 1, 1);
plot(t_total, noisy_signal);
title('Noisy ASK Signal (Time Domain)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
noisy_signal_fft = fft(noisy_signal);
noisy_signal_fft = fftshift(noisy_signal_fft);
f_noisy_signal =
(-length(noisy_signal_fft)/2:length(noisy_signal_fft)/2-
1)*(Fs_mod/length(noisy_signal_fft)); % Frequency vector for
noisy signal
plot(f_noisy_signal, abs(noisy_signal_fft));
title('Noisy ASK Signal (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% Plot the Demodulated ASK Signal (Stream of Bits in Time


Domain)
figure;
subplot(2, 1, 1);
stairs(t_total(1:length(demodulated_bits)),
demodulated_bits); % Plot binary stream (0s and 1s)
title('Demodulated ASK Signal (Stream of Bits)');
xlabel('Time (s)');
ylabel('Bit Value (0 or 1)');

% Plot the Decoded Signal (Time and Frequency)


figure;
subplot(2, 1, 1);
stairs((0:length(decoded_signal)-1)/Fs, decoded_signal);
title('Decoded Signal (Time Domain)');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
decoded_signal_fft = fft(decoded_signal);
decoded_signal_fft = fftshift(decoded_signal_fft);
f_decoded_signal =
(-length(decoded_signal_fft)/2:length(decoded_signal_fft)/2-
1)*(Fs/length(decoded_signal_fft)); % Frequency vector for
decoded signal
plot(f_decoded_signal, abs(decoded_signal_fft));
title('Decoded Signal (Frequency Domain)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

% --- BER Calculation ---


% Compare the original binary stream and the demodulated
binary stream
transmitted_binary_stream = binary_stream; % The original
transmitted stream
received_binary_stream = reshape(demodulated_binary', [],
1); % The received binary stream after demodulation

% Ensure both streams are of the same length (trim the


longer one if needed)
min_len = min(length(transmitted_binary_stream),
length(received_binary_stream));
transmitted_binary_stream =
transmitted_binary_stream(1:min_len);
received_binary_stream = received_binary_stream(1:min_len);

% Calculate the number of bit errors


num_errors = sum(transmitted_binary_stream ~=
received_binary_stream);

% Calculate BER
BER = num_errors / length(transmitted_binary_stream);

% Display BER
disp(['Bit Error Rate (BER): ', num2str(BER)]);

You might also like