1 | module analyser
|
---|
2 | (
|
---|
3 | input wire clk, reset,
|
---|
4 | input wire data_ready,
|
---|
5 | input wire [1:0] uwt_flag,
|
---|
6 | input wire [11:0] uwt_data,
|
---|
7 | input wire [11:0] threshold,
|
---|
8 | output wire peak_ready,
|
---|
9 | output wire [11:0] peak
|
---|
10 | );
|
---|
11 |
|
---|
12 | reg [1:0] state_reg, state_next;
|
---|
13 | reg [3:0] counter_reg, counter_next;
|
---|
14 | reg peak_ready_reg, peak_ready_next;
|
---|
15 | reg [11:0] peak_reg, peak_next;
|
---|
16 | reg [15:0] buffer [15:0];
|
---|
17 | wire [15:0] sample;
|
---|
18 | wire [11:0] baseline;
|
---|
19 |
|
---|
20 | integer i;
|
---|
21 |
|
---|
22 | assign sample = {4'd0, uwt_data};
|
---|
23 | assign baseline = buffer[15][15:4];
|
---|
24 |
|
---|
25 | always @(posedge clk)
|
---|
26 | begin
|
---|
27 | if (reset)
|
---|
28 | begin
|
---|
29 | state_reg <= 2'd0;
|
---|
30 | counter_reg <= 4'd0;
|
---|
31 | peak_ready_reg <= 1'b0;
|
---|
32 | peak_reg <= 12'd0;
|
---|
33 |
|
---|
34 | for(i = 0; i <= 15; i = i + 1)
|
---|
35 | begin
|
---|
36 | buffer[i] <= 12'd0;
|
---|
37 | end
|
---|
38 | end
|
---|
39 | else
|
---|
40 | begin
|
---|
41 | state_reg <= state_next;
|
---|
42 | counter_reg <= counter_next;
|
---|
43 | peak_ready_reg <= peak_ready_next;
|
---|
44 | peak_reg <= peak_next;
|
---|
45 |
|
---|
46 | if (data_ready & uwt_flag[1])
|
---|
47 | begin
|
---|
48 | for(i = 0; i < 15; i = i + 1)
|
---|
49 | begin
|
---|
50 | buffer[i+1] <= buffer[i] + sample;
|
---|
51 | end
|
---|
52 | buffer[0] <= sample;
|
---|
53 | end
|
---|
54 | end
|
---|
55 | end
|
---|
56 |
|
---|
57 | always @*
|
---|
58 | begin
|
---|
59 | state_next = state_reg;
|
---|
60 | counter_next = counter_reg;
|
---|
61 | peak_ready_next = peak_ready_reg;
|
---|
62 | peak_next = peak_reg;
|
---|
63 | case (state_reg)
|
---|
64 | 0: // skip first 16 samples
|
---|
65 | begin
|
---|
66 | peak_next = 12'd0;
|
---|
67 | peak_ready_next = 1'b0;
|
---|
68 | if (data_ready)
|
---|
69 | begin
|
---|
70 | counter_next = counter_reg + 4'd1;
|
---|
71 | if (&counter_reg)
|
---|
72 | begin
|
---|
73 | state_next = 2'd1;
|
---|
74 | end
|
---|
75 | end
|
---|
76 | end
|
---|
77 |
|
---|
78 | 1: // skip first 16 minima
|
---|
79 | begin
|
---|
80 | if (data_ready & uwt_flag[1])
|
---|
81 | begin
|
---|
82 | counter_next = counter_reg + 4'd1;
|
---|
83 | if (&counter_reg)
|
---|
84 | begin
|
---|
85 | state_next = 2'd2;
|
---|
86 | end
|
---|
87 | end
|
---|
88 | end
|
---|
89 |
|
---|
90 | 2: // calculate peak height
|
---|
91 | begin
|
---|
92 | if (data_ready & uwt_flag[0])
|
---|
93 | begin
|
---|
94 | peak_next = (uwt_data > baseline) ? (uwt_data - baseline) : 12'd0;
|
---|
95 | peak_ready_next = (peak_next > threshold);
|
---|
96 | end
|
---|
97 | else
|
---|
98 | begin
|
---|
99 | peak_ready_next = 1'b0;
|
---|
100 | end
|
---|
101 | end
|
---|
102 |
|
---|
103 | default:
|
---|
104 | begin
|
---|
105 | peak_next = 12'd0;
|
---|
106 | peak_ready_next = 1'b0;
|
---|
107 | state_next = 2'd0;
|
---|
108 | end
|
---|
109 | endcase
|
---|
110 | end
|
---|
111 |
|
---|
112 | assign peak_ready = peak_ready_reg;
|
---|
113 | assign peak = peak_reg;
|
---|
114 | endmodule
|
---|