-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
234 lines (124 loc) · 6.17 KB
/
main.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
%% Task 1: Pulse Code Modulation
% Clear the workspace and figures
clear, close all
% Define message signal
% message signal properties
message_frequency_cosine = 100; % in Hertz (Hz)
message_frequency_sine = 25; % in Hertz (Hz)
message_amplitude_cosine = -1; % (usually) in Volts (V)
message_amplitude_sine = 1; % (usually) in Volts (V)
% the component with the highest frequency carries the bandwidth
bandwidth = max([message_frequency_cosine, message_frequency_sine]); % in Hertz (Hz)
sampling_frequency = 2 * bandwidth; % Nyquist sampling frequency in Hertz (Hz)
% initialize the time vector
signal_duration = 2; % in seconds (s)
% sampling starts from t_0 = Ts
time = (1:(signal_duration * sampling_frequency - 1)) ./ sampling_frequency;
% calculate the message signal
message_cosine = message_amplitude_cosine .* cos(2 * pi * message_frequency_cosine * time);
message_sine = message_amplitude_sine .* sin(2 * pi * message_frequency_sine * time);
message = message_cosine + message_sine;
% calculate the new message signal with higher time sensitivity
new_frequency = 1e6;
new_time = (1:(signal_duration * new_frequency - 1)) ./ new_frequency;
new_message_cosine = message_amplitude_cosine .* cos(2 * pi * message_frequency_cosine * new_time);
new_message_sine = message_amplitude_sine .* sin(2 * pi * message_frequency_sine * new_time);
new_message = new_message_cosine + new_message_sine;
% calculate the negative and positive peak values using new message signal for more accuracy
amplitude_pmax = max(new_message); % find the positive peak amplitude
amplitude_nmax = min(new_message); % find the negative peak amplitude
% Define Pulse Code Modulation (PCM) properties
L = 128; % number of quantization levels
no_of_bits = int32(log2(L)); % number of bits (2^n = L)
level_limits = linspace(amplitude_pmax, amplitude_nmax, L + 1); % L + 1 level limits
% take averages of the successive level limits to obtain quantization levels
quantization_levels = (level_limits(1:L) + level_limits(2:end)) / 2;
% Define the actual modulator
% define the quantized message signal
quantized_message = zeros('like', message);
% go through the modulation loop
for i = 1:length(message)
amplitude_diffs = abs(message(i) - quantization_levels);
% find the first index where the amplitude difference is greater than or equal to the one before it
% if such index doesn't exist then set the quantized level as L - 1,
% otherwise set the quantized level as the index before the one we have just found
quantized_level = find(amplitude_diffs(2:end) >= amplitude_diffs(1:(L - 1)), 1);
if isempty(quantized_level)
quantized_message(i) = L - 1;
elseif quantized_level > 1
quantized_message(i) = quantized_level - 1;
end
end
% Display the binary representation of the first 10 samples on the screen
output = arrayfun(@(x) dec2bin(x, no_of_bits), quantized_message(1:10), 'UniformOutput', false);
fprintf('%s', output{1})
fprintf('-%s', output{2:end})
fprintf('\n')
%% Task 2: Delta Modulation
% Clear the workspace and figures
clear, close all
% Define message signal
% initialize the time vector
sampling_frequency = 1e6; % in Hertz (Hz)
signal_duration = 2; % in seconds (s)
time = (0:(signal_duration * sampling_frequency - 1)) ./ sampling_frequency;
% message signal properties
message_frequency_cosine = 100; % in Hertz (Hz)
message_frequency_sine = 25; % in Hertz (Hz)
message_amplitude_cosine = -1; % (usually) in Volts (V)
message_amplitude_sine = 1; % (usually) in Volts (V)
% calculate the message signal
message_cosine = message_amplitude_cosine .* cos(2 * pi * message_frequency_cosine * time);
message_sine = message_amplitude_sine .* sin(2 * pi * message_frequency_sine * time);
message = message_cosine + message_sine;
% Define Delta Modulation (DM) properties
% the component with the highest frequency carries the bandwidth
bandwidth = max([message_frequency_cosine, message_frequency_sine]); % in Hertz (Hz)
% define delta sampling frequency as 4 times the Nyquist rate
nyquist_rate_factor = 4;
delta_sampling_frequency = nyquist_rate_factor * 2 * bandwidth; % in Hertz (Hz)
delta_epsilon = 0.2; % define step size in seconds (s)
% Define the actual modulator
% define sample time array
delta_time = (0:(signal_duration * delta_sampling_frequency - 1)) ./ delta_sampling_frequency;
% calculate the sampled message signal
sampled_message_cosine = message_amplitude_cosine * cos(2 * pi * message_frequency_cosine * delta_time);
sampled_message_sine = message_amplitude_sine * sin(2 * pi * message_frequency_sine * delta_time);
sampled_message = sampled_message_cosine + sampled_message_sine;
% allocate memory
prediction = zeros(length(sampled_message), 1);
modulated = zeros('like', prediction);
% go through the modulation loop
for i = 2:length(sampled_message)
amplitude_diff = sampled_message(i) - prediction(i - 1);
modulated(i) = (2 * double(amplitude_diff > 0) - 1) * delta_epsilon;
prediction(i) = prediction(i - 1) + modulated(i);
end
% Print the first 20 samples on the screen
output = int32(modulated(2:21) > 0);
fprintf('%d', output(1))
fprintf('-%d', output(2:end))
fprintf('\n')
% Plot obtained signals (optional)
% define plot parameters
plot_time = 0.1;
% plot the message and predicted signals
subplot(2, 1, 1)
plot(time, message, 'b')
% stairs(delta_time, sampled_message, 'c--')
hold on
stairs(delta_time, prediction, 'r')
xlabel('Time (s)')
ylabel('Amplitude')
title('Message and Predicted Signals', 'Color', 'r')
legend('Message signal', 'Predicted signal', 'Location', 'Southwest')
axis([0, plot_time, -2, 2])
grid on
% plot the transmitted DM signal
subplot(2, 1, 2)
stem(delta_time, modulated, 'b')
xlabel('Time (s)')
ylabel('Amplitude')
title('Transmitted DM Signal', 'Color', 'r')
axis([0, plot_time, -2*delta_epsilon, 2*delta_epsilon])
grid on