File: iq_correction.vhd

package info (click to toggle)
bladerf 0.2016.06-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,624 kB
  • sloc: ansic: 44,880; vhdl: 11,945; python: 1,062; xml: 1,028; tcl: 952; makefile: 654; sh: 537; csh: 18; cpp: 9
file content (276 lines) | stat: -rw-r--r-- 9,271 bytes parent folder | download | duplicates (8)
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
library ieee;
    use ieee.std_logic_1164.all;
    use ieee.numeric_std.all;
    use ieee.math_real.all;



entity iq_correction is
    generic(
        INPUT_WIDTH : positive := 16;
        OUTPUT_WIDTH: positive := 16;
        DC_WIDTH    : positive := 16;
        GAIN_WIDTH  : positive := 16;
        PHASE_WIDTH : positive := 16;

        Q           : positive := 12

        );

    port(
        clock       :   in std_logic;
        reset       :   in std_logic;

        --input signal
        in_real     :   in signed(INPUT_WIDTH-1 downto 0);
        in_imag     :   in signed(INPUT_WIDTH-1 downto 0);
        in_valid    :   in std_logic;

        --output signal
        out_real    :   out signed(OUTPUT_WIDTH-1 downto 0);
        out_imag    :   out signed(OUTPUT_WIDTh-1 downto 0);
        out_valid   :   out std_logic;

        --correction signals
        dc_real     :   in signed(DC_WIDTH-1 downto 0);
        dc_imag     :   in signed(DC_WIDTH-1 downto 0);

        gain :   in signed(GAIN_WIDTH-1 downto 0);
        phase :   in signed(PHASE_WIDTH-1 downto 0);

        correction_valid:   in std_logic
    );
end entity;

architecture rx of iq_correction is

    signal dc_balanced_real : signed(INPUT_WIDTH-1 downto 0);
    signal dc_balanced_imag : signed(INPUT_WIDTH-1 downto 0);
    signal dc_balanced_valid : std_logic;

    signal gain_corrected_real : signed(INPUT_WIDTH-1 downto 0);
    signal gain_corrected_imag : signed(INPUT_WIDTH-1 downto 0);
    signal gain_corrected_valid : std_logic;

    signal phase_corrected_real : signed(OUTPUT_WIDTH-1 downto 0);
    signal phase_corrected_imag : signed(OUTPUT_WIDTH-1 downto 0);
    signal phase_corrected_valid : std_logic;

    signal phase_correction : signed(OUTPUT_WIDTH-1 downto 0);


begin
    --
    U_correction_lookup : entity work.tan_table
    generic map(
            STEPS => 2**Q,
            ANGLE => MATH_PI*(10.0/180.0)
    )
    port map
    (
        clock => clock,
        reset => reset,

        phase => phase,
        phase_valid => correction_valid,

        y => phase_correction,
        y_valid => open
    );

    --apply DC correction to real and imag
    apply_dc : process(clock, reset)
    begin 
        if reset = '1' then
            --
            dc_balanced_real <= (others => '0');
            dc_balanced_imag <= (others => '0');
            dc_balanced_valid <= '0';
        elsif rising_edge(clock) then
            dc_balanced_valid <= '0';
            if in_valid = '1' then
                dc_balanced_real <= in_real - dc_real;
                dc_balanced_imag <= in_imag - dc_imag;
                dc_balanced_valid <= '1';
            end if;
        end if;
    end process;

    --apply gain correction to real
    apply_gain : process(clock, reset)
    begin
        if reset = '1' then
            gain_corrected_real <= (others => '0');
            gain_corrected_imag <= (others => '0');
            gain_corrected_valid <= '0';
        elsif rising_edge(clock) then
            gain_corrected_valid <= '0';
            if dc_balanced_valid = '1' then
                gain_corrected_real <= resize(shift_right(dc_balanced_real*gain,Q),gain_corrected_real'length);
                gain_corrected_imag <= dc_balanced_imag;
                gain_corrected_valid <= '1';
            end if;
        end if;
    end process;

    --apply phase correction to imag
    apply_phase : process(clock, reset)
        variable register_offset_real : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_offset_imag : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_real : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_imag : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_valid : std_logic;

    begin
        if reset = '1' then
            phase_corrected_real <= (others => '0');
            phase_corrected_imag <= (others => '0');
            phase_corrected_valid <= '0';
            register_offset_real :=  (others => '0');
            register_offset_imag :=  (others => '0');
            register_real :=  (others => '0');
            register_imag :=  (others => '0');
            register_valid := '0';
        elsif rising_edge(clock) then
            phase_corrected_valid <= '0';
            if register_valid = '1' then
                phase_corrected_real <= register_real + register_offset_real;
                phase_corrected_imag <= register_imag + register_offset_imag;
                phase_corrected_valid <= '1';
            end if;

            register_valid := '0';
            if gain_corrected_valid = '1' then
                register_offset_real := resize(shift_right(gain_corrected_imag*phase_correction,Q),phase_corrected_real'length);
                register_offset_imag := resize(shift_right(gain_corrected_real*phase_correction,Q),phase_corrected_imag'length);
                register_real := gain_corrected_real;
                register_imag := gain_corrected_imag;
                register_valid := '1';
            end if;
        end if;
    end process;

    out_real <= phase_corrected_real;
    out_imag <= phase_corrected_imag;
    out_valid <= phase_Corrected_valid;

end architecture;



architecture tx of iq_correction is

    signal dc_balanced_real : signed(INPUT_WIDTH-1 downto 0);
    signal dc_balanced_imag : signed(INPUT_WIDTH-1 downto 0);
    signal dc_balanced_valid : std_logic;

    signal gain_corrected_real : signed(INPUT_WIDTH-1 downto 0);
    signal gain_corrected_imag : signed(INPUT_WIDTH-1 downto 0);
    signal gain_corrected_valid : std_logic;

    signal phase_corrected_real : signed(OUTPUT_WIDTH-1 downto 0);
    signal phase_corrected_imag : signed(OUTPUT_WIDTH-1 downto 0);
    signal phase_corrected_valid : std_logic;

    signal phase_correction : signed(OUTPUT_WIDTH-1 downto 0);

begin

    --apply gain correction to real
    apply_gain : process(clock, reset)
    begin
        if reset = '1' then
            gain_corrected_real <= (others => '0');
            gain_corrected_imag <= (others => '0');
            gain_corrected_valid <= '0';
        elsif rising_edge(clock) then
            gain_corrected_valid <= '0';
            if in_valid = '1' then
                gain_corrected_real <= resize(shift_right(in_real*gain,Q),gain_corrected_real'length);
                gain_corrected_imag <= in_imag;
                gain_corrected_valid <= '1';
            end if;
        end if;
    end process;


    --
    U_phase_correction_lookup : entity work.tan_table
    generic map(
            STEPS => 2**Q,
            ANGLE => MATH_PI*(10.0/180.0)
    )
    port map
    (
        clock => clock,
        reset => reset,

        phase => phase,
        phase_valid => correction_valid,

        y => phase_correction,
        y_valid => open
    );

    --apply phase correction to imag
    apply_phase : process(clock, reset)
        variable register_offset_real : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_offset_imag : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_real : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_imag : signed(OUTPUT_WIDTH-1 downto 0);
        variable register_valid : std_logic;

    begin
        if reset = '1' then
            phase_corrected_real <= (others => '0');
            phase_corrected_imag <= (others => '0');
            phase_corrected_valid <= '0';
            register_offset_real :=  (others => '0');
            register_offset_imag :=  (others => '0');
            register_real :=  (others => '0');
            register_imag :=  (others => '0');
            register_valid := '0';
        elsif rising_edge(clock) then
            phase_corrected_valid <= '0';
            if register_valid = '1' then
                phase_corrected_real <= register_real + register_offset_real;
                phase_corrected_imag <= register_imag + register_offset_imag;
                phase_corrected_valid <= '1';
            end if;

            register_valid := '0';
            if gain_corrected_valid = '1' then
                register_offset_real := resize(shift_right(gain_corrected_imag*phase_correction,Q),phase_corrected_real'length);
                register_offset_imag := resize(shift_right(gain_corrected_real*phase_correction,Q),phase_corrected_imag'length);
                register_real := gain_corrected_real;
                register_imag := gain_corrected_imag;
                register_valid := '1';
            end if;
        end if;
    end process;



    --apply DC correction to real and imag
    apply_dc : process(clock, reset)
    begin 
        if reset = '1' then
            --
            dc_balanced_real <= (others => '0');
            dc_balanced_imag <= (others => '0');
            dc_balanced_valid <= '0';
        elsif rising_edge(clock) then
            dc_balanced_valid <= '0';
            if phase_corrected_valid = '1' then
                dc_balanced_real <= phase_corrected_real - dc_real;
                dc_balanced_imag <= phase_corrected_imag - dc_imag;
                dc_balanced_valid <= '1';
            end if;
        end if;
    end process;

    out_real <= dc_balanced_real;
    out_imag <= dc_balanced_imag;
    out_valid <= dc_balanced_valid;

end architecture;