-
Notifications
You must be signed in to change notification settings - Fork 24
/
modulation.m
63 lines (53 loc) · 1.8 KB
/
modulation.m
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
function modulated = modulation(modulation_name, bit_stream, Rb, k, amp, freq)
% INPUT:
% modulation_name = can be 'BASK', 'BPSK', 'QPSK'
% bit_stream = string of bits
% Rb = bit rate
% k = samples per bit
% amp = amplitude of the modulated signal
% scaler for BPSK and QPSK, vector ([max min]) for BASK
% freq = carrier frequency of the modulated signal
% OUTPUT:
% modulated = modulated signal of the bit stream
modulated = [];
% converting string to vector
N = length(bit_stream);
% line coding
line_code = repelem(bit_stream, k);
Tb = 1/Rb; % bit duration
Fs = k * Rb; % sampling frequency
Ts = 1 / Fs;
time = 0 : Ts : N*Tb-Ts;
switch modulation_name
case 'BASK'
carrier = sin(2*pi*freq*time);
a1 = amp(1);
a0 = amp(2);
line_code = a1 * line_code + a0 .* (line_code==0);
modulated = line_code .* carrier;
case 'BPSK'
a = amp(1);
carrier = sin(2*pi*freq*time);
line_code = a .* (line_code == 1) + (-a) .* (line_code == 0);
modulated = line_code .* carrier;
case 'QPSK'
M = 4;
bn = log2(M);
t = 0 : Ts : bn*Tb-Ts;
% Gray code generation
x = 0 : M-1;
code = bin2gray(x, 'psk', M);
gray_code = de2bi(code, bn, 'left-msb');
for i = 1:bn:length(bit_stream)
sym = bit_stream(i:i+bn-1);
index = find(ismember(gray_code, sym, 'rows'), 1);
val = bi2de(gray_code(index, :));
phi = 2 * pi * val / M;
modulated = [modulated amp(1)*sin(2*pi*freq*t + phi)];
end
otherwise
fprintf('\n');
warning(['"', modulation_name, '" Modulation is not supported! ONLY BASK, BPSK, QPSK are supported.']);
return;
end
end