% Clear workspace and figures
clc; clear; close all;
% Parameters
N = 10; % Number of bits
bp = 0.000001; % Duration per bit (1 microsecond)
fc = 2e6; % Carrier frequency (2 MHz)
fs = 100 * fc; % Sampling frequency
% 1. Generate Random Binary Data (Bits)
bit_stream = randi([0 1], 1, N);
% 2. Expand Bits for Plotting (NRZ waveform)
t_bit = linspace(0, bp, 100);
digital_signal = [];
for i = 1:N
if bit_stream(i) == 1
digital_signal = [digital_signal ones(1, 100)];
else
digital_signal = [digital_signal zeros(1, 100)];
end
end
t_total = linspace(0, N*bp, N*100);
% 3. BPSK Modulation (Convert 0->-1, 1->+1 and multiply with carrier)
bpsk_signal = [];
for i = 1:N
t_carrier = linspace(0, bp, 100);
carrier = cos(2 * pi * fc * t_carrier);
if bit_stream(i) == 1
bpsk_signal = [bpsk_signal carrier]; % 0 degrees phase shift
else
bpsk_signal = [bpsk_signal -carrier]; % 180 degrees phase shift
end
end
% 4. Plotting the Waveforms
figure;
subplot(2,1,1);
plot(t_total, digital_signal, 'LineWidth', 2);
title('Input Binary Data (Digital Signal)');
xlabel('Time (s)'); ylabel('Amplitude');
grid on; axis([0 N*bp -0.5 1.5]);
subplot(2,1,2);
plot(t_total, bpsk_signal, 'LineWidth', 1.5);
title('BPSK Modulated Signal');
xlabel('Time (s)'); ylabel('Amplitude');
grid on; axis([0 N*bp -1.5 1.5]);
To embed this project on your website, copy the following code and paste it into your website's HTML: