OpenCores
URL https://opencores.org/ocsvn/lp_iir_filter/lp_iir_filter/trunk

Subversion Repositories lp_iir_filter

[/] [lp_iir_filter/] [trunk/] [Testbench/] [filtertb_i.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 unicore
---------------------------------------------------------------------
2
----                                                             ----
3
----  IIR Filter IP core                                         ----
4
----                                                             ----
5
----  Authors: Anatoliy Sergienko, Volodya Lepeha                ----
6
----  Company: Unicore Systems http://unicore.co.ua              ----
7
----                                                             ----
8
----  Downloaded from: http://www.opencores.org                  ----
9
----                                                             ----
10
---------------------------------------------------------------------
11
----                                                             ----
12
---- Copyright (C) 2006-2010 Unicore Systems LTD                 ----
13
---- www.unicore.co.ua                                           ----
14
---- o.uzenkov@unicore.co.ua                                     ----
15
----                                                             ----
16
---- This source file may be used and distributed without        ----
17
---- restriction provided that this copyright statement is not   ----
18
---- removed from the file and that any derivative work contains ----
19
---- the original copyright notice and the associated disclaimer.----
20
----                                                             ----
21
---- THIS SOFTWARE IS PROVIDED "AS IS"                           ----
22
---- AND ANY EXPRESSED OR IMPLIED WARRANTIES,                    ----
23
---- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED                  ----
24
---- WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT              ----
25
---- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.        ----
26
---- IN NO EVENT SHALL THE UNICORE SYSTEMS OR ITS                ----
27
---- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,            ----
28
---- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL            ----
29
---- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT         ----
30
---- OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,               ----
31
---- DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                 ----
32
---- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,              ----
33
---- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT              ----
34
---- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING                 ----
35
---- IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,                 ----
36
---- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.          ----
37
----                                                             ----
38
---------------------------------------------------------------------
39
---------------------------------------------------------------------------------------------------
40
--
41
-- Description :            Testbench for digital filters
42
--
43
---------------------------------------------------------------------------------------------------
44
 
45
 
46
library IEEE;
47
use IEEE.STD_LOGIC_1164.all;
48
use IEEE.MATH_REAL.all;
49
 
50
entity FilterTB is
51
        generic(fsampl:integer := 2000;
52
                fstrt: integer:=0;
53
                deltaf:integer:=20;
54
                maxdelay:integer:=100;
55
                slowdown:integer:=3;
56
                magnitude:real:=1000.0
57
                );
58
        port(
59
                CLK : in STD_LOGIC;
60
                RST : in STD_LOGIC;
61
                RERSP : in INTEGER;
62
                IMRSP : in INTEGER;
63
                REO : out INTEGER;
64
                IMO : out INTEGER;
65
                FREQ : out INTEGER;
66
                MAGN:out INTEGER;
67
                LOGMAGN:out REAL;
68
                PHASE: out REAL ;
69
                ENA: inout STD_LOGIC
70
                );
71
end FilterTB;
72
 
73
architecture FilterTB of FilterTB is
74
        signal freqi,REOI,IMOI: integer;
75
        signal nextf: STD_LOGIC;
76
        signal rdy: STD_LOGIC;
77
        signal  rdyd:std_logic;
78
        signal phasei,phaseo: real;
79
begin
80
 
81
        SINGEN:process (CLK,RST)
82
                variable phase:real:=0.0;
83
                variable i:integer:=0;
84
        begin
85
                if ( RST='1' ) then
86
                        REOi<=0;
87
                        IMOi<=0;
88
                        i:=0;
89
                        phase:=0.0;
90
                        nextf<='0';
91
                elsif ( CLK='1' and CLK'event ) then
92
                        if ( ENA='1' ) then
93
                                REOi<=integer(magnitude*COS(2.0*MATH_PI*phase ));
94
                                IMOi<=integer(magnitude*SIN(2.0*MATH_PI*phase));
95
                                phase:=    phase+real(freqi)/real(fsampl);
96
                                i:=i+1;
97
                                if ( i=maxdelay) then
98
                                        i:=0;
99
                                        phase:=0.0;
100
                                        nextf<='1';
101
                                else
102
                                        nextf<='0';
103
                                end if;
104
                                if ( i>=maxdelay-4 and i<=maxdelay-1  ) then
105
                                        rdy<='1';
106
                                else
107
                                        rdy<='0';
108
                                end if;
109
                        end if;
110
                end if;
111
        end process;
112
        REO<=REOi;
113
        IMO<=IMOi;
114
 
115
        SLOWER:process(CLK,RST)
116
                variable i:integer:=0;
117
        begin
118
                if ( RST='1' ) then
119
                        i:=0;
120
                        ENA<='0';
121
                elsif ( CLK='1' and CLK'event ) then
122
                        i:=i+1;
123
                        if ( i=slowdown) then
124
                                i:=0;
125
                                ENA<='1';
126
                        else
127
                                ENA<='0';
128
                        end if;
129
                end if;
130
        end process;
131
 
132
        NEW_FREQ:process(CLK,RST)
133
        begin
134
                if ( RST='1') then
135
                        freqi<=fstrt ;
136
                elsif ( CLK='1' and CLK'event ) then
137
                        if ( ENA='1' and nextf='1') then
138
                                freqi<=freqi+deltaf;
139
                        end if;
140
                end if;
141
        end process;
142
 
143
        FREQ<=freqi;
144
 
145
        MEASURE:process(CLK,RST)
146
                variable re,im,rei,imi,mag,phasei,phaseo: real:=0.0;
147
                variable ct:natural;
148
        begin
149
                if ( RST='1') then
150
                        MAGN<=0;
151
                        LOGMAGN<=0.0;
152
                        PHASE<=0.0;
153
                elsif ( CLK='1' and CLK'event) then
154
                        if ( ENA='1' ) then
155
                                rdyd<=rdy;
156
                                if       rdy='1' then
157
 
158
 
159
 
160
                                        re:= real(RERSP)  ;
161
                                        im:= real(IMRSP) ;
162
                                        rei:= real(REOi)  ;
163
                                        imi:= real(IMOi) ;
164
                                        if re=0.0 then re:=0.00000001; end if;
165
                                        if rei=0.0 then rei:=0.00000001; end if;
166
                                        if rdyd='0' then
167
                                                mag:=SQRT(re*re+im*im);
168
                                                ct:=0;
169
                                        elsif rdyd='1' then
170
                                                mag:=mag +      SQRT(re*re+im*im);
171
                                                ct:=ct+1;
172
                                        end if;
173
                                        if ( mag=0.0) then
174
                                                mag:=0.01;
175
                                        end if;
176
                                        if ct=3   then
177
                                                MAGN<=integer(mag/4.0);
178
                                                LOGMAGN<=20.0*LOG10(mag/magnitude/4.0);
179
                                                PHASEi:=ARCTAN(imi,rei);
180
                                                PHASEo:=ARCTAN(im,re);
181
                                                PHASE<=PHASEo-PHASEi;
182
                                                if (PHASEo-PHASEi >math_pi) then
183
                                                        PHASE<=PHASEo-PHASEi-2.0*math_pi;
184
                                                else
185
                                                        PHASE<=PHASEo-PHASEi;
186
                                                end if;
187
                                                if   (PHASEo-PHASEi < (- math_pi)) then
188
                                                        PHASE<=PHASEo-PHASEi+2.0*math_pi;
189
                                                end if;
190
                                        end if;
191
                                end if;
192
                        end if;
193
                end if;
194
        end process;
195
 
196
 
197
end FilterTB;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.