-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomm_code.m
More file actions
123 lines (75 loc) · 1.94 KB
/
comm_code.m
File metadata and controls
123 lines (75 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
clc;
clear;
close all;
%% Message Input
message = 'HELLO THIS IS A BPSK COMMUNICATION SYSTEM PROJECT';
%% Text to Binary
ascii = double(message);
binary_matrix = dec2bin(ascii,8);
bits = reshape(binary_matrix' - '0',1,[]);
%% BPSK Modulation
bpsk = 2*bits - 1;
%% Add Noise
noise = 0.2 * randn(size(bpsk));
received = bpsk + noise;
%% Receiver
recovered_bits = received > 0;
%% BER Calculation
errors = sum(bits ~= recovered_bits);
BER = errors / length(bits);
%% Binary to Text
recovered_matrix = reshape(recovered_bits,8,[])';
recovered_ascii = bin2dec(num2str(recovered_matrix));
recovered_message = char(recovered_ascii)';
%% Display Results
disp('Original Message:')
disp(message)
disp('Recovered Message:')
disp(recovered_message)
disp(['Total Bits Transmitted = ', num2str(length(bits))]);
disp(['Total Bits Recovered = ', num2str(length(recovered_bits))]);
fprintf('Number of Errors = %d\n', errors);
fprintf('BER = %.3f\n', BER);
%% Plots
figure;
subplot(3,1,1);
stairs(bits,'LineWidth',2);
title('Binary Data');
ylim([-0.5 1.5]);
grid on;
subplot(3,1,2);
stairs(bpsk,'LineWidth',2);
title('BPSK Modulated Signal');
ylim([-1.5 1.5]);
grid on;
subplot(3,1,3);
plot(received,'o-','LineWidth',1.5);
title('Received Signal with Noise');
grid on;
figure;
subplot(2,1,1)
stairs(bits,'LineWidth',2)
title('Original Bits')
ylim([-0.5 1.5])
grid on
subplot(2,1,2)
stairs(recovered_bits,'LineWidth',2)
title('Recovered Bits')
ylim([-0.5 1.5])
grid on
%% BER vs Noise Analysis
noise_levels = [0.1 0.3 0.5 0.7 1.0];
BER_values = zeros(size(noise_levels));
for i = 1:length(noise_levels)
noise = noise_levels(i) * randn(size(bpsk));
received = bpsk + noise;
recovered_bits = received > 0;
errors = sum(bits ~= recovered_bits);
BER_values(i) = errors / length(bits);
end
figure;
plot(noise_levels, BER_values, '-o', 'LineWidth', 2);
title('BER vs Noise Level');
xlabel('Noise Level');
ylabel('Bit Error Rate (BER)');
grid on;