1 |
2 |
danv |
-------------------------------------------------------------------------------
|
2 |
3 |
danv |
--
|
3 |
|
|
-- Copyright 2020
|
4 |
2 |
danv |
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
|
5 |
|
|
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
|
6 |
3 |
danv |
--
|
7 |
|
|
-- Licensed under the Apache License, Version 2.0 (the "License");
|
8 |
|
|
-- you may not use this file except in compliance with the License.
|
9 |
|
|
-- You may obtain a copy of the License at
|
10 |
|
|
--
|
11 |
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
12 |
|
|
--
|
13 |
|
|
-- Unless required by applicable law or agreed to in writing, software
|
14 |
|
|
-- distributed under the License is distributed on an "AS IS" BASIS,
|
15 |
|
|
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 |
|
|
-- See the License for the specific language governing permissions and
|
17 |
|
|
-- limitations under the License.
|
18 |
2 |
danv |
--
|
19 |
|
|
-------------------------------------------------------------------------------
|
20 |
|
|
|
21 |
|
|
library ieee, common_pkg_lib;
|
22 |
|
|
use IEEE.std_logic_1164.all;
|
23 |
|
|
use common_pkg_lib.common_pkg.all;
|
24 |
|
|
|
25 |
|
|
package fft_pkg is
|
26 |
|
|
|
27 |
|
|
-- FFT parameters for pipelined FFT (fft_pipe), parallel FFT (fft_par) and wideband FFT (fft_wide)
|
28 |
|
|
type t_fft is record
|
29 |
|
|
use_reorder : boolean; -- = false for bit-reversed output, true for normal output
|
30 |
|
|
use_fft_shift : boolean; -- = false for [0, pos, neg] bin frequencies order, true for [neg, 0, pos] bin frequencies order in case of complex input
|
31 |
|
|
use_separate : boolean; -- = false for complex input, true for two real inputs
|
32 |
|
|
nof_chan : natural; -- = default 0, defines the number of channels (=time-multiplexed input signals): nof channels = 2**nof_chan
|
33 |
|
|
wb_factor : natural; -- = default 1, wideband factor
|
34 |
|
|
twiddle_offset : natural; -- = default 0, twiddle offset for PFT sections in a wideband FFT
|
35 |
|
|
nof_points : natural; -- = 1024, N point FFT
|
36 |
|
|
in_dat_w : natural; -- = 8, number of input bits
|
37 |
|
|
out_dat_w : natural; -- = 13, number of output bits
|
38 |
|
|
out_gain_w : natural; -- = 0, output gain factor applied after the last stage output, before requantization to out_dat_w
|
39 |
|
|
stage_dat_w : natural; -- = 18, data width used between the stages(= DSP multiplier-width)
|
40 |
|
|
guard_w : natural; -- = 2, guard used to avoid overflow in first FFT stage, compensated in last guard_w nof FFT stages.
|
41 |
|
|
-- on average the gain per stage is 2 so guard_w = 1, but the gain can be 1+sqrt(2) [Lyons section
|
42 |
|
|
-- 12.3.2], therefore use input guard_w = 2.
|
43 |
|
|
guard_enable : boolean; -- = true when input needs guarding, false when input requires no guarding but scaling must be
|
44 |
|
|
-- skipped at the last stage(s) compensate for input guard (used in wb fft with pipe fft section
|
45 |
|
|
-- doing the input guard and par fft section doing the output compensation)
|
46 |
|
|
stat_data_w : positive; -- = 56
|
47 |
|
|
stat_data_sz : positive; -- = 2
|
48 |
|
|
end record;
|
49 |
|
|
|
50 |
|
|
constant c_fft : t_fft := (true, false, true, 0, 4, 0, 1024, 8, 14, 0, c_dsp_mult_w, 2, true, 56, 2);
|
51 |
|
|
|
52 |
|
|
-- Check consistancy of the FFT parameters
|
53 |
|
|
function fft_r2_parameter_asserts(g_fft : t_fft) return boolean; -- the return value is void, because always true or abort due to failure
|
54 |
|
|
|
55 |
|
|
-- Definitions for fft slv array (an array can not have unconstraint elements, so choose sufficiently wide 32 bit slv elements)
|
56 |
|
|
subtype t_fft_slv_arr is t_slv_32_arr; -- use subtype to ease interfacing to existing types and to have central definition for rtwo components
|
57 |
|
|
constant c_fft_slv_w : natural := 32; -- match slv width of t_fft_slv_arr
|
58 |
|
|
function to_fft_svec(n : integer) return std_logic_vector; -- map to c_fft_slv_w wide slv, no need for to_rtwo_uvec, because natural is subtype of integer
|
59 |
|
|
function resize_fft_uvec(vec : std_logic_vector) return std_logic_vector; -- map to c_fft_slv_w wide slv
|
60 |
|
|
function resize_fft_svec(vec : std_logic_vector) return std_logic_vector; -- map to c_fft_slv_w wide slv
|
61 |
|
|
|
62 |
|
|
-- FFT shift swaps right and left half of bin axis to shift zero-frequency component to center of spectrum
|
63 |
|
|
function fft_shift(bin : std_logic_vector) return std_logic_vector;
|
64 |
|
|
function fft_shift(bin, w : natural) return natural;
|
65 |
|
|
|
66 |
|
|
end package fft_pkg;
|
67 |
|
|
|
68 |
|
|
package body fft_pkg is
|
69 |
|
|
|
70 |
|
|
function fft_r2_parameter_asserts(g_fft : t_fft) return boolean is
|
71 |
|
|
begin
|
72 |
|
|
-- nof_points
|
73 |
|
|
assert g_fft.nof_points=2**true_log2(g_fft.nof_points) report "fft_r2: nof_points must be a power of 2" severity failure;
|
74 |
|
|
-- wb_factor
|
75 |
|
|
assert g_fft.wb_factor=2**true_log2(g_fft.wb_factor) report "fft_r2: wb_factor must be a power of 2" severity failure;
|
76 |
|
|
-- use_reorder
|
77 |
|
|
if g_fft.use_reorder=false then
|
78 |
|
|
assert g_fft.use_separate=false report "fft_r2 : without use_reorder there cannot be use_separate for two real inputs" severity failure;
|
79 |
|
|
assert g_fft.use_fft_shift=false report "fft_r2 : without use_reorder there cannot be use_fft_shift for complex input" severity failure;
|
80 |
|
|
end if;
|
81 |
|
|
-- use_separate
|
82 |
|
|
if g_fft.use_separate=true then
|
83 |
|
|
assert g_fft.use_fft_shift=false report "fft_r2 : with use_separate there cannot be use_fft_shift for two real inputs" severity failure;
|
84 |
|
|
end if;
|
85 |
|
|
return true;
|
86 |
|
|
end;
|
87 |
|
|
|
88 |
|
|
function to_fft_svec(n : integer) return std_logic_vector is
|
89 |
|
|
begin
|
90 |
|
|
return RESIZE_SVEC(TO_SVEC(n, 32), c_fft_slv_w);
|
91 |
|
|
end;
|
92 |
|
|
|
93 |
|
|
function resize_fft_uvec(vec : std_logic_vector) return std_logic_vector is
|
94 |
|
|
begin
|
95 |
|
|
return RESIZE_UVEC(vec, c_fft_slv_w);
|
96 |
|
|
end;
|
97 |
|
|
|
98 |
|
|
function resize_fft_svec(vec : std_logic_vector) return std_logic_vector is
|
99 |
|
|
begin
|
100 |
|
|
return RESIZE_SVEC(vec, c_fft_slv_w);
|
101 |
|
|
end;
|
102 |
|
|
|
103 |
|
|
function fft_shift(bin : std_logic_vector) return std_logic_vector is
|
104 |
|
|
constant c_w : natural := bin'length;
|
105 |
|
|
variable v_bin : std_logic_vector(c_w-1 downto 0) := bin;
|
106 |
|
|
begin
|
107 |
|
|
return not v_bin(c_w-1) & v_bin(c_w-2 downto 0); -- invert MSbit for fft_shift
|
108 |
|
|
end;
|
109 |
|
|
|
110 |
|
|
function fft_shift(bin, w : natural) return natural is
|
111 |
|
|
begin
|
112 |
|
|
return TO_UINT(fft_shift(TO_UVEC(bin, w)));
|
113 |
|
|
end;
|
114 |
|
|
|
115 |
|
|
end fft_pkg;
|
116 |
|
|
|