Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

verilog code to find max and min in an input..

Status
Not open for further replies.

MR.sam

Newbie level 3
Joined
May 21, 2016
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
30
I want to find max and min in input file, read from a memory. This input file containsize 1000 decimal sample values. I have written the following code to find max and min by comparing with a threshold.


Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
module max_min(input clk, input [15:0]din, output [15:0]dout);
    reg [15:0]max=0;
    reg [15:0]min=0;
    always @(posedge clk)  begin
        if($signed(din)>max)
            max=din;
        else if($signed(din)<min)
            min=din;
    end
    assign dout=max;`
endmodule


The problem in above code is that for example the if condition becomes true for (eg) the 2nd cycle sample value, then max will be assigned the 2nd sample value as max sample value. Now let us consider that input din has max sample value at the 15th cycle of all the 1000 samples. So after the 15th cycle max will contain that max value and the output dout will contain max value after 15th cycle and before 15th cycle it will contain the maximum of the first 14 sample values. I want my output i.e max register to contain only the maximum value i.e the 15th value.
 
Last edited by a moderator:

Presently your design has only one max register, min register is causing no design output and will be ignored in synthesis.

max is updated each time din is exceeding the previously stored max value. Obviously, the new value can't be known before din arrives. (There's no known Verilog syntax to accomplish a prophecy, I believe)

So how do you want the design to behave differently? You can e.g. have a separate max_final output register that copies the internal max value when the 1000 samples have been processed. Requiring a respective store input.

Side remark, there should be an asynchronous reset or synchronous init input to initialize the min/max registers.
 
  • Like
Reactions: MR.sam

    MR.sam

    Points: 2
    Helpful Answer Positive Rating
Presently your design has only one max register, min register is causing no design output and will be ignored in synthesis.

max is updated each time din is exceeding the previously stored max value. Obviously, the new value can't be known before din arrives. (There's no known Verilog syntax to accomplish a prophecy, I believe)

So how do you want the design to behave differently? You can e.g. have a separate max_final output register that copies the internal max value when the 1000 samples have been processed. Requiring a respective store input.

Side remark, there should be an asynchronous reset or synchronous init input to initialize the min/max registers.

thanks for yours reply..dear i want that my max reg contain only the maximum value among all 1000 sample values..can u suggest me some modification in this code?

- - - Updated - - -

thanks for yours reply..dear i want that my max reg contain only the maximum value among all 1000 sample values..can u suggest me some modification in this code?

hi FvM..thaks alot for yours reply..dear i want to find a single max and min value in input din, so further i can normalize my input signal din by following formula..
din_n=din-min/max-min;
 

i want to find a single max and min value in input din, so further i can normalize my input signal din by following formula..
din_n=din-min/max-min;

There are essentially two options to know min and max beforehand.

- read the file twice. Determine min and max in first pass and perform normalization in second pass
- read all data into a memory array
 

There are essentially two options to know min and max beforehand.

- read the file twice. Determine min and max in first pass and perform normalization in second pass
- read all data into a memory array

i have written the complete code as follows for normalization but it didn't give the result which i expect

Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module max_min(input clk, input [15:0]din, output [15:0]dout);
reg [15:0]max=0;
reg [15:0]min=0;
reg [15:0]din1;
reg [15:0]din2;
always @(posedge clk) begin
if($signed(din)>max)
max=din;
else if($signed(din)<min)
min=din;
end
always @(posedge clk) begin
if(rst)
din1<=0;
din2<=0;
else
din1<=(din-min);
din2<=din1>>(max-min);
end
 
assign dout=din2;
endmodule..


plzz help me out by suggesting correcting this code..
 
Last edited by a moderator:

You don't have a memory array, so I presume you want to read the file twice.

Code:
din2<=din1>>(max-min);
I don't see how logical right shift should be related to division respectively normalization.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top