X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/e17437f985b1c955b3a2e3cab4f9146db54631a1..3b2fee43ea7d9e08b7729d662d8d010ee0e2a2e3:/fpga/min_max_tracker.v diff --git a/fpga/min_max_tracker.v b/fpga/min_max_tracker.v new file mode 100644 index 00000000..516f0d6f --- /dev/null +++ b/fpga/min_max_tracker.v @@ -0,0 +1,65 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2014 iZsh +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// track min and max peak values (envelope follower) +// +// NB: the min value (resp. max value) is updated only when the next high peak +// (resp. low peak) is reached/detected, since you can't know it isn't a +// local minima (resp. maxima) until then. +// This also means the peaks are detected with an unpredictable delay. +// This algorithm can't therefore be used directly for realtime peak detections, +// but it can be used as a simple envelope follower. +module min_max_tracker(input clk, input [7:0] adc_d, input [7:0] threshold, + output [7:0] min, output [7:0] max); + + reg [7:0] min_val = 255; + reg [7:0] max_val = 0; + reg [7:0] cur_min_val = 255; + reg [7:0] cur_max_val = 0; + reg [1:0] state = 0; + + always @(posedge clk) + begin + case (state) + 0: + begin + if (cur_max_val >= ({1'b0, adc_d} + threshold)) + state <= 2; + else if (adc_d >= ({1'b0, cur_min_val} + threshold)) + state <= 1; + if (cur_max_val <= adc_d) + cur_max_val <= adc_d; + else if (adc_d <= cur_min_val) + cur_min_val <= adc_d; + end + 1: + begin + if (cur_max_val <= adc_d) + cur_max_val <= adc_d; + else if (({1'b0, adc_d} + threshold) <= cur_max_val) begin + state <= 2; + cur_min_val <= adc_d; + max_val <= cur_max_val; + end + end + 2: + begin + if (adc_d <= cur_min_val) + cur_min_val <= adc_d; + else if (adc_d >= ({1'b0, cur_min_val} + threshold)) begin + state <= 1; + cur_max_val <= adc_d; + min_val <= cur_min_val; + end + end + endcase + end + + assign min = min_val; + assign max = max_val; + +endmodule