1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2014 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // track min and max peak values (envelope follower)
10 // NB: the min value (resp. max value) is updated only when the next high peak
11 // (resp. low peak) is reached/detected, since you can't know it isn't a
12 // local minima (resp. maxima) until then.
13 // This also means the peaks are detected with an unpredictable delay.
14 // This algorithm therefore can't be used directly for realtime peak detections,
15 // but it can be used as a simple envelope follower.
16 module min_max_tracker(input clk
, input [7:0] adc_d
, input [7:0] threshold
,
17 output [7:0] min
, output [7:0] max
);
19 reg [7:0] min_val
= 255;
20 reg [7:0] max_val
= 0;
21 reg [7:0] cur_min_val
= 255;
22 reg [7:0] cur_max_val
= 0;
30 if (cur_max_val
>= ({1'b0, adc_d
} + threshold
))
32 else if (adc_d
>= ({1'b0, cur_min_val
} + threshold
))
34 if (cur_max_val
<= adc_d
)
36 else if (adc_d
<= cur_min_val
)
41 if (cur_max_val
<= adc_d
)
43 else if (({1'b0, adc_d
} + threshold
) <= cur_max_val
) begin
46 max_val
<= cur_max_val
;
51 if (adc_d
<= cur_min_val
)
53 else if (adc_d
>= ({1'b0, cur_min_val
} + threshold
)) begin
56 min_val
<= cur_min_val
;