-
Notifications
You must be signed in to change notification settings - Fork 0
/
pll_iq.m
352 lines (303 loc) · 8.73 KB
/
pll_iq.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
clc
clear all
close all
%Loop Parameters%
digital_bandwidth= 2*pi/100;
damping_factor=sqrt(2)/2;
ki= (4*digital_bandwidth*digital_bandwidth)/...
(1+2*damping_factor*digital_bandwidth+digital_bandwidth*digital_bandwidth);
kp= (4*damping_factor*digital_bandwidth)/...
(1+2*damping_factor*digital_bandwidth+digital_bandwidth*digital_bandwidth);
%Input Accumulator%
normalized_freq_1=0.05; normalized_freq_2=0.1;
phase_series=[normalized_freq_1*ones(1,100) normalized_freq_2*ones(1,100)];
%Phase Detector Phase Error%
ph=filter(1,[1 -1],phase_series);
input_complex_sinusoid= exp(1i*2*pi*ph);
ph_sv = zeros(1,200);
ph_sv_1= zeros(1,200);
phase_series_sv = zeros(1,200);
int_reg = 0;
accumulator = 0;
%Loop Filter and Phase Accumulator%
for n=1:200
product =input_complex_sinusoid(n)*exp(-1i*2*pi*accumulator);
ph_1 = angle(product)/(2*pi);
ph_sv(n) = ph_1;
int_reg = int_reg + ki*ph_1;
normalized_freq = kp*ph_1+int_reg;
phase_series_sv(n) = normalized_freq;
ph_sv_1(n) = accumulator;
accumulator = accumulator+normalized_freq;
end
output_complex_sinusoid=exp(1i*2*pi*ph_sv_1);
%Phase Error%
phase_error=ph-ph_sv_1;
figure(1)
subplot(3,1,1)
plot(phase_error)
grid on
title('Phase Detector Phase Error, Input to Loop Filter')
xlabel('Time Index, Output Clock')
ylabel('Phase Error')
subplot(3,1,2)
plot(ph_sv)
hold on
plot(phase_series_sv,'r')
hold off
grid on
title('Loop Filter Output, Input To Phase Accumulator')
xlabel('Time Index, Output Clock')
ylabel('Loop Control')
subplot(3,1,3)
plot(ph)
hold on
plot(ph_sv_1,'r')
hold off
grid on
title('Phase Series of the Two Phase Accumulators')
xlabel('Time Index, Input Clock')
ylabel('Phase Profile')
%Real and Imaginary Parts of i/p and o/p Sinusoids%
s=exp(1i*2*pi*ph_sv_1);
figure(2)
subplot(2,1,1)
plot(real(input_complex_sinusoid))
hold on
plot(real(output_complex_sinusoid),'r')
hold off
grid on
title('Real Part of the i/p and o/p Sinusoids')
xlabel('Time Index')
ylabel('Amplitude')
subplot(2,1,2)
plot(imag(input_complex_sinusoid))
hold on
plot(imag(output_complex_sinusoid),'r')
hold off
grid on
title('Imaginary Part of the i/p and o/p Sinusoids')
xlabel('Time Index')
ylabel('Amplitude')
Fs = 4; %Sampling Frequency of the Filter
Fd = 1; %Sampling Frequency of Digital i/p Signal
delay =10; % Filter Group Delay
r=0.50;
num=rcosine(Fd,Fs,'sqrt',r,delay); %Nyquist Filter
num_1=num/max(num); %Shaping Filter Scaling
num_2=num_1/(num_1*num_1');%Proper Scaling for Matched Filter
%Forming 16-QAM Symbols%
Number_of_Symbols=1000;
Signal_16QAM=(floor(4*rand(1,Number_of_Symbols))-1.5)/1.5+...
1j*(floor(4*rand(1,Number_of_Symbols))-1.5)/1.5;
%1-to-4 upsampling filters (Shaping Filters)%
h1=reshape([num_1 zeros(1,3)],4,21);
register=zeros(1,20);
x=zeros(1,4*Number_of_Symbols); % Shaping Filter o/p Array
m=0; % o/p Clock Index
for n=1:Number_of_Symbols
register=[Signal_16QAM(n) register(1:20)]; % Shift i/p Sample into Shaping Filter
for k=1:4
x(m+k)=register*h1(k,:)';
end
m=m+4;
end
%Eye Diagram%
figure(3)
subplot(2, 1, 1)
plot(0,0)
hold on
for n=(1:4:4*Number_of_Symbols-4)
plot((-1:1/2:1),real(x(n:n+4)))
end
hold off
grid on
title('Eye Diagram of the Modulated Signal')
%Constellation Diagram%
subplot(2,1,2)
plot(x(1:4:4*Number_of_Symbols),'rx')
grid on
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('Constellation Diagram of the Modulated Signal')
sigma=0.02 ;
complex_noise=(sigma*randn(1,4* Number_of_Symbols))+...
1j*(sigma*randn(1,4* Number_of_Symbols));
complex_input_sequence=x+complex_noise;
rotated_sequence= complex_input_sequence.*exp(1j*2*pi/36); %Rotating Complex i/p Sequence
%Eye Diagram%
figure(4)
subplot(2, 1, 1)
plot(0,0)
hold on
for n=(1:4:4*Number_of_Symbols-4)
plot((-1:1/2:1),real(rotated_sequence(n:n+4)))
end
hold off
grid on
title('Eye Diagram of the Rotated Signal')
%Constellation Diagram%
subplot(2,1,2)
plot(rotated_sequence(1:4:4*Number_of_Symbols),'rx')
grid on
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('Constellation Diagram of the Rotated Signal')
y=conv(rotated_sequence,num_2);
%Eye Diagram%
figure(5)
subplot(2, 1, 1)
plot(0,0)
hold on
for n=(1:4:4*Number_of_Symbols-4)
plot((-1:1/2:1),real(y(n:n+4)))
end
hold off
grid on
title('Eye Diagram of the Matched Filter Output Signal')
%Constellation Diagram%
subplot(2,1,2)
plot(y(1:4:4*Number_of_Symbols),'rx')
grid on
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('Constellation Diagram of the Matched Filter Output Signal')
flag=1;
mf_register=zeros(1,80);
for n=1:4*Number_of_Symbols
slicer_rotated_sequence=rotated_sequence(n);
mf_register=[slicer_rotated_sequence mf_register(1:80)];
slicer_rotated_sequence_1(n)=mf_register*num_2';
if flag==1 %detect count
slicer_rotated_sequence_1_real=real(slicer_rotated_sequence_1(n));
slicer_rotated_sequence_1_real_det=1;
if slicer_rotated_sequence_1_real<0
slicer_rotated_sequence_1_real_det=-1;
end
if abs(slicer_rotated_sequence_1_real)<2/3;
slicer_rotated_sequence_1_real_det=slicer_rotated_sequence_1_real_det/3;
end
slicer_rotated_sequence_1_imag=imag(slicer_rotated_sequence_1(n));
slicer_rotated_sequence_1_imag_det=1;
if slicer_rotated_sequence_1_imag<0
slicer_rotated_sequence_1_imag_det=-1;
end
if abs(slicer_rotated_sequence_1_imag)<2/3;
slicer_rotated_sequence_1_imag_det=slicer_rotated_sequence_1_imag_det/3;
end
sum(n)=slicer_rotated_sequence_1_real_det+1j*slicer_rotated_sequence_1_imag_det;
conjugate_product(n)=slicer_rotated_sequence_1(n)*...
(slicer_rotated_sequence_1_real_det-1j*slicer_rotated_sequence_1_imag_det);
%ATAN%
output_angle(n)=angle(conjugate_product(n))/(2*pi);
end
flag=flag+1;
if flag==5
flag=1;
end
end
figure(6)
subplot(3,1,1)
plot(slicer_rotated_sequence_1(1:4:end),'x')
axis('square')
hold on
grid on
plot(sum(1:4:end),'rx')
hold off
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('2D Slicer i/p and o/p Constellation');
subplot(3,1,2)
plot(conjugate_product(1:4:end),'x')
grid on
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('Conjugate Product Constellation');
subplot(3,1,3)
plot(output_angle);
grid on
xlabel('Time')
ylabel('Amplitude')
title('Output Time Series of ATAN')
%Loop Parameters%
digital_bandwidth= 2*pi/1000;
damping_factor=sqrt(2)/2;
%damping_factor=1*damping_factor;
ki= (4*digital_bandwidth*digital_bandwidth)/...
(1+2*damping_factor*digital_bandwidth+digital_bandwidth*digital_bandwidth);
kp= (4*damping_factor*digital_bandwidth)/...
(1+2*damping_factor*digital_bandwidth+digital_bandwidth*digital_bandwidth);
phase_accumulator=0;
flag=1;
register_1=zeros(1,80);
int_reg=0;
reg_hold=0;
for n= 1:4*Number_of_Symbols
rotated_sequence_1=rotated_sequence(n)*exp(-1j*2*pi*phase_accumulator);
register_1=[rotated_sequence_1 register_1(1:80)];
rotated_sequence_2(n)=register_1*num_2';
if flag==1 %detect count
rotated_sequence_2_real=real(rotated_sequence_2(n));
rotated_sequence_2_real_det=1;
if rotated_sequence_2_real<0
rotated_sequence_2_real_det=-1;
end
if abs(rotated_sequence_2_real)<2/3;
rotated_sequence_2_real_det=rotated_sequence_2_real_det/3;
end
rotated_sequence_2_imag=imag(rotated_sequence_2(n));
rotated_sequence_2_imag_det=1;
if rotated_sequence_2_imag<0
rotated_sequence_2_imag_det=-1;
end
if abs(rotated_sequence_2_imag)<2/3;
rotated_sequence_2_imag_det=rotated_sequence_2_imag_det/3;
end
sum(n)=rotated_sequence_2_real_det+1j*rotated_sequence_2_imag_det;
conjugate_product(n)=rotated_sequence_2(n)*...
(rotated_sequence_2_real_det-1j*rotated_sequence_2_imag_det);
%ATAN%
output_angle=angle(conjugate_product(n))/(2*pi);
int_reg=int_reg+ki*output_angle;
reg_hold=int_reg+kp*output_angle;
output_angle_1(n)=output_angle;
end
filter_reg(n)=reg_hold;
flag=flag+1;
if flag==5
flag=1;
end
phase_accumulator=phase_accumulator+reg_hold;
end
figure(7)
subplot(3,1,1)
plot(rotated_sequence_2(1:4:end),'x')
axis('square')
hold on
grid on
plot(sum(1:4:end),'rx')
hold off
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('De-Spinned Constellation');
subplot(3,1,2)
plot(conjugate_product(1:4:end),'x')
grid on
axis('square')
xlabel('Inphase')
ylabel('Quadrature')
title('Conjugate Product Constellation');
subplot(3,1,3)
plot(output_angle_1);
grid on
xlabel('Time')
ylabel('Amplitude')
title('Output Time Series of ATAN')
%pause(15);
%close all;