-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxCancelAlgorithm.m
More file actions
224 lines (201 loc) · 8.43 KB
/
txCancelAlgorithm.m
File metadata and controls
224 lines (201 loc) · 8.43 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
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% txCancelAlgorithm %
% %
% Filename: txCancelAlgorithm.m %
% Creation Date: 11/04/2015 %
% Author: Edward Keehr %
% %
% Copyright Superlative Semiconductor LLC 2021 %
% This source describes Open Hardware and is licensed under the CERN-OHL-P v2 %
% You may redistribute and modify this documentation and make products %
% using it under the terms of the CERN-OHL-P v2 (https:/cern.ch/cern-ohl). %
% This documentation is distributed WITHOUT ANY EXPRESS OR IMPLIED %
% WARRANTY, INCLUDING OF MERCHANTABILITY, SATISFACTORY QUALITY %
% AND FITNESS FOR A PARTICULAR PURPOSE. Please see the CERN-OHL-P v2 %
% for applicable conditions. %
% %
% This is the same algorithm as from testMatchingNetwork25Algo100615H. %
% It requires that the step matrix and the cap matrix be loaded onto the %
% next highest level of the simulation. %
% %
% 110515 - Add gain adjustment. %
% 111715 - Clean up and add comments while placing in the release directory %
% %
% This file was created to be part of the closed loop SDM-TX cancellation %
% simulation. As such, it has a persistent memory of the capacitor settings %
% The intent is to call this file once per baseband sampling period %
% and the function will spit out the next capacitor value setting. %
% In real life, the output of this circuit will need to connect to an SPI %
% controller. %
% %
% 123015 - Altered to reflect the minimal-LUT implementation that was %
% eventually adopted. %
% 022116 - Modified to support the 2-bit Jacobian Matrix %
% 031516 - Modified to support multiplication by simple LUTs. %
% 100817 - Modified to match current digital implementation, support for %
% data-driven gain changes, etc. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cap_vec_return=txCancelAlgorithm(dc_i,dc_q,step_mat_A_qtz,step_mat_B_qtz,step_mat_C_qtz,step_mat_D_qtz,reset)
persistent cap1_state=16384; %%% Keep the capacitor settings as persistent variables so that we can update them with the next information sample.
persistent cap3_state=16384;
persistent prev_error=0;
persistent mag_delta_c1=0;
persistent loop_mode=0;
persistent burn=0;
persistent c1_flag=0;
persistent c3_flag=0;
persistent fail_ctr=0;
persistent lna_gain_state=0;
persistent gain_bits=0;
%%% Add provision to reset the capacitor vector before running the algorithm.
if(reset)
cap1_state=16384; %%% Keep the capacitor settings as persistent variables so that we can update them with the next information sample.
cap3_state=16384;
prev_error=0;
mag_delta_c1=0;
loop_mode=0;
burn=0;
c1_flag=0;
c3_flag=0;
fail_ctr=0;
lna_gain_state=0;
gain_bits=0;
return;
endif
%%% Perform the gradient descent calculation.
curr_error=max(abs(dc_i),abs(dc_q))+floor(min(abs(dc_i),abs(dc_q))/4);
gain_bits_temp=gain_bits;
while (gain_bits_temp)
curr_error=floor(curr_error/2); %This may be too much for gain_bits=7
gain_bits_temp-=1;
end
cap_vec(1)=bitand(31,floor(cap1_state/(2^10)));
cap_vec(2)=bitand(31,floor(cap1_state/(2^5)));
cap_vec(3)=bitand(31,floor(cap3_state/(2^10)));
cap_vec(4)=bitand(31,floor(cap3_state/(2^5)));
disp(sprintf("LM: %d CE: %d\t DCI:%d\t DCQ:%d\t C1:%d\t C2:%d\t C3:%d\t C4:%d\t GB:%d\t",loop_mode,curr_error,dc_i,dc_q,cap_vec(1),cap_vec(2),cap_vec(3),cap_vec(4),gain_bits));
step_vec=[0 0].';
if(burn==1)
if(loop_mode==0)
prev_error=curr_error;
if(c1_flag==0)
if(gain_bits==7)
step_vec(1)=32;
else
step_vec(1)=prev_error;
endif
else
if(gain_bits==7)
step_vec(1)=-32;
else
step_vec(1)=-prev_error;
endif
endif
elseif(loop_mode==1)
if(curr_error >= prev_error)
if(c1_flag==0)
if(gain_bits==7)
step_vec(1)=-32;
else
step_vec(1)=-prev_error;
endif
else
if(gain_bits==7)
step_vec(1)=32;
else
step_vec(1)=prev_error;
endif
endif
c1_flag=!c1_flag;
endif
elseif(loop_mode==2)
prev_error=curr_error;
if(c3_flag==0)
if(gain_bits==7)
step_vec(2)=32;
else
step_vec(2)=prev_error;
endif
else
if(gain_bits==7)
step_vec(2)=-32;
else
step_vec(2)=-prev_error;
endif
endif
elseif(loop_mode==3)
if(curr_error >= prev_error)
if(c3_flag==0)
if(gain_bits==7)
step_vec(2)=-32;
else
step_vec(2)=-prev_error;
endif
else
if(gain_bits==7)
step_vec(2)=32;
else
step_vec(2)=prev_error;
endif
endif
c3_flag=!c3_flag;
endif
endif
endif
if(!((lna_gain_state==2) && !(curr_error > 9))) %This is a bit hokey. Best idea may be to stop when we reach the minimum at highest gain? Same as curr err < 9
cap1_state+=floor(step_vec(1));
cap3_state+=floor(step_vec(2));
end
lna_gain_state_new=lna_gain_state;
if(loop_mode==3)
if(lna_gain_state==0)
if(curr_error > 16383)
cap1_state=16384;
cap2_state=16384;
elseif(!(curr_error > 255))
lna_gain_state_new=1;
endif
elseif(lna_gain_state==1)
if(curr_error > 2047)
lna_gain_state_new=0;
elseif(!(curr_error > 31))
lna_gain_state_new=2;
endif
elseif(lna_gain_state==2)
if(curr_error > 127)
lna_gain_state_new=1;
endif
else
error("Problem in tx cancel");
endif
endif
lna_gain_state=lna_gain_state_new;
%%% Make sure that any steps do not exceed the bounds of the capacitor control values.
cap1_state=max(min(cap1_state,32767),0);
cap3_state=max(min(cap3_state,32767),0);
%%% Break apart the control values for the four capacitors so that they can be individually addressed over SPI
cap_vec(1)=bitand(31,floor(cap1_state/(2^10)));
cap_vec(2)=bitand(31,floor(cap1_state/(2^5)));
cap_vec(3)=bitand(31,floor(cap3_state/(2^10)));
cap_vec(4)=bitand(31,floor(cap3_state/(2^5)));
if(lna_gain_state==0)
cap_vec(5)=10^(-23/20);
gain_bits=0;
elseif(lna_gain_state==1)
cap_vec(5)=10^(-4/20);
gain_bits=3;
elseif(lna_gain_state==2)
cap_vec(5)=10^(20/20);
gain_bits=7;
else
error("Someone made a boo boo with LNA gain state transitions");
endif
%disp(sprintf("LM: %d CE: %d\t SV1:%d\t SV2:%d\t DCI:%d\t DCQ:%d\t C1:%d\t C2:%d\t C3:%d\t C4:%d\t",loop_mode,curr_error,step_vec(1),step_vec(2),dc_i,dc_q,cap_vec(1),cap_vec(2),cap_vec(3),cap_vec(4)));
if(loop_mode==3 && burn==0)
burn=1;
endif
loop_mode=mod(loop_mode+1,4);
cap_vec_return=cap_vec;
endfunction