Verilog 找出任意六个数中的最大、次最大和第三最大值以及三个数对应的序号

思路:1、把六个数看成是两组的3个数

             2、对每组的三个数进行从大到小的排序

            3、找出有序的两组数中最大、次最大和第三最大值

 

//模块1:对三个数按照从大到小排序

//例:若 i_t_1st_max=20          i_t_2nd_max=5                 i_t_3th_max=30

                 i_Num_t_1st_max=1   i_Num_t_2nd_max=2       i_Num_t_3th_max=3

          则 o_all_1st_max=30       o_all_2nd _max=20          o_all_3th_max=5

               o_index_1stmax=3      o_index_2ndmax=1          o_index_3thmax=2

module rank3data(

input  i_rst_n,

         i_start

 //输入的三个数

input signed[`EXTENDWORDSIZE:0] i_t_1st_max,

                                                      i_t_2nd_max,

                                                      i_t_3th_max,

 //输入的三个数对应的序列号

input [`INDEX:0]  i_Num_t_1st_max,

                        i_Num_t_2nd_max,

                        i_Num_t_3th_max,  

 //排序后输出的三个数对应的序号

output reg [`INDEX:0] o_index_1stmax,

                               o_index_2ndmax,

                               o_index_3thmax,

 //排序后输出的三个数

output reg signed[`EXTENDWORDSIZE:0] o_all_1st_max,

                                                             o_all_2nd_max,

                                                             o_all_3th_max

                            );

         always @(i_rst_n or i_start or i_t_1st_max or i_t_2nd_max or i_t_3th_max or i_Num_t_1st_max or i_Num_t_2nd_max or i_Num_t_3th_max) begin

              if(!i_rst_n)begin

                    o_all_1st_max=0;

                   o_all_2nd_max=0;

                    o_all_3th_max=0;

                 o_index_1stmax=0;

                 o_index_2ndmax=0;

                  o_index_3thmax=0;

               end

              else if(i_start)begin

                     if(i_t_1st_max>=i_t_2nd_max)begin

                       if(i_t_1st_max>=i_t_3th_max) begin

                         if(i_t_2nd_max>=i_t_3th_max) begin 

                                    o_all_1st_max=i_t_1st_max;

                                    o_all_2nd_max=i_t_2nd_max;

                                    o_all_3th_max=i_t_3th_max;

                                  o_index_1stmax=i_Num_t_1st_max;

                                  o_index_2ndmax=i_Num_t_2nd_max;

                                  o_index_3thmax=i_Num_t_3th_max;

                             end

                             else begin

                                    o_all_1st_max=i_t_1st_max;

                                    o_all_2nd_max=i_t_3th_max;

                                    o_all_3th_max=i_t_2nd_max;

                                    o_index_1stmax=i_Num_t_1st_max;

                                    o_index_2ndmax=i_Num_t_3th_max;

                                    o_index_3thmax=i_Num_t_2nd_max;

                               end

                              end

                              else begin 

                                    o_all_1st_max=i_t_3th_max;

                                    o_all_2nd_max=i_t_1st_max;

                                    o_all_3th_max=i_t_2nd_max;

                                    o_index_1stmax=i_Num_t_3th_max;

                                    o_index_2ndmax=i_Num_t_1st_max;

                                    o_index_3thmax=i_Num_t_2nd_max;

                              end

                            end

                     else begin

                     if(i_t_1st_maxbegin

                        if(i_t_2nd_max>=i_t_3th_max) begin   

                                    o_all_1st_max=i_t_2nd_max;

                                    o_all_2nd_max=i_t_3th_max;

                                    o_all_3th_max=i_t_1st_max;

                                    o_index_1stmax=i_Num_t_2nd_max;

                                    o_index_2ndmax=i_Num_t_3th_max;

                                    o_index_3thmax=i_Num_t_1st_max;

                             end

                             else begin 

                                    o_all_1st_max=i_t_3th_max;

                                    o_all_2nd_max=i_t_2nd_max;

                                    o_all_3th_max=i_t_1st_max;

                                    o_index_1stmax=i_Num_t_3th_max;

                                    o_index_2ndmax=i_Num_t_2nd_max;

                                    o_index_3thmax=i_Num_t_1st_max;

                               end

                       end

                       else begin 

                                    o_all_1st_max=i_t_2nd_max;

                                    o_all_2nd_max=i_t_1st_max;

                                    o_all_3th_max=i_t_3th_max;

                                    o_index_1stmax=i_Num_t_2nd_max;

                                    o_index_2ndmax=i_Num_t_1st_max;

                                    o_index_3thmax=i_Num_t_3th_max;

                       end

                     end

                     end

               end

endmodule

 

//模块2:两组3个有序的数比较得到最大、次最大和第三最大值已经对应序号

//例:若i_t_1st_max=30          i_t_2nd_max=20           i_t_3th_max=5

              i_b_1st_max=10          i_b_2nd_max=8            i_b_3th_max= -30

              i_Num_t_1st_max=1   i_Num_t_2nd_max=2   i_Num_t_3th_max=3

              i_Num_t_1st_max=4   i_Num_t_2nd_max=5   i_Num_t_3th_max=6

        则o_all_1st_max=30        o_all_2nd _max=20      o_all_3th_max=10

            o_index_1stmax=1       o_index_2ndmax=2      o_index_3thmax=4

module two3rank_compare(

input  i_rst_n,

         i_start, 

input signed[`EXTENDWORDSIZE:0] i_t_1st_max,

                                                    i_t_2nd_max,

                                                     i_t_3th_max,

                                                    i_b_1st_max,

                                                    i_b_2nd_max,

                                                     i_b_3th_max,  

input [`INDEX:0i_Num_t_1st_max,

                       i_Num_t_2nd_max,

                       i_Num_t_3th_max,

                       i_Num_b_1st_max,

                       i_Num_b_2nd_max,

                       i_Num_b_3th_max,

        

output reg [`INDEX:0] o_index_1stmax,

                               o_index_2ndmax,

                               o_index_3thmax,

output reg signed[`EXTENDWORDSIZE:0] o_all_1st_max,

                                                             o_all_2nd_max,

                                                             o_all_3th_max

                            );

       always @(i_rst_n or i_start or i_t_1st_max or i_t_2nd_max or i_t_3th_max or i_b_1st_max or i_b_2nd_max or i_b_3th_max or i_Num_t_1st_max or i_Num_t_2nd_max or i_Num_t_3th_max or i_Num_b_1st_max or i_Num_b_2nd_max or i_Num_b_3th_max)begin

              if(!i_rst_n) begin

                   o_all_1st_max=0;

                   o_all_2nd_max=0;

                   o_all_3th_max=0;

                   o_index_1stmax=0;

                   o_index_2ndmax=0;

                   o_index_3thmax=0;

               end

              else if(i_start)begin

              if(i_t_3th_max>=i_b_1st_max)begin //a b c

                  o_all_1st_max=i_t_1st_max;

                  o_all_2nd_max=i_t_2nd_max;

                  o_all_3th_max=i_t_3th_max;

                 o_index_1stmax=i_Num_t_1st_max;

                 o_index_2ndmax=i_Num_t_2nd_max;

                 o_index_3thmax=i_Num_t_3th_max;

              end

              else if(i_t_2nd_max>=i_b_1st_max)begin // a b d

                             o_all_1st_max=i_t_1st_max;

                             o_all_2nd_max=i_t_2nd_max;

                             o_all_3th_max=i_b_1st_max;

                        o_index_1stmax=i_Num_t_1st_max;

                        o_index_2ndmax=i_Num_t_2nd_max;

                        o_index_3thmax=i_Num_b_1st_max;

                      end

                      else if(i_t_1st_max>=i_b_1st_max)begin//a d b

                            if(i_t_2nd_max>=i_b_2nd_max)begin

                                    o_all_1st_max=i_t_1st_max;

                                    o_all_2nd_max=i_b_1st_max; 

                                    o_all_3th_max=i_t_2nd_max;

                              o_index_1stmax=i_Num_t_1st_max;

                              o_index_2ndmax=i_Num_b_1st_max;

                              o_index_3thmax=i_Num_t_2nd_max;

                             end

                              else begin//a d e

                                    o_all_1st_max=i_t_1st_max;

                                    o_all_2nd_max=i_b_1st_max;

                                    o_all_3th_max=i_b_2nd_max;

                               o_index_1stmax=i_Num_t_1st_max;

                               o_index_2ndmax=i_Num_b_1st_max;

                               o_index_3thmax=i_Num_b_2nd_max;  

                              end

                             end

                       else if(i_t_1st_max>=i_b_2nd_max)begin

                          if(i_t_2nd_max>=i_b_2nd_max)begin// d a b

                                    o_all_1st_max=i_b_1st_max ;

                                    o_all_2nd_max=i_t_1st_max;

                                    o_all_3th_max=i_t_2nd_max;

                              o_index_1stmax=i_Num_b_1st_max;

                              o_index_2ndmax=i_Num_t_1st_max;

                              o_index_3thmax=i_Num_t_2nd_max;

                             end

                               else begin //d a e 

                                           o_all_1st_max=i_b_1st_max;       

                                           o_all_2nd_max=i_t_1st_max;       

                                           o_all_3th_max=i_b_2nd_max;       

                                     o_index_1stmax=i_Num_b_1st_max;   

                                     o_index_2ndmax=i_Num_t_1st_max;   

                                    o_index_3thmax=i_Num_b_2nd_max;   

                               end

                       end

                       else if(i_t_1st_max>=i_b_3th_max)begin //d e a  

                                     o_all_1st_max=i_b_1st_max;         

                                    o_all_2nd_max=i_b_2nd_max;         

                                    o_all_3th_max=i_t_1st_max;         

                              o_index_1stmax=i_Num_b_1st_max;     

                              o_index_2ndmax=i_Num_b_2nd_max;     

                              o_index_3thmax=i_Num_t_1st_max;     

                       end

                       else begin     // d e f  

                           o_all_1st_max=i_b_1st_max;           

                           o_all_2nd_max=i_b_2nd_max;           

                           o_all_3th_max=i_b_3th_max;           

                           o_index_1stmax=i_Num_b_1st_max;       

                           o_index_2ndmax=i_Num_b_2nd_max;       

                           o_index_3thmax=i_Num_b_3th_max; 

                       end

                      end

              end

endmodule

 

//顶层模块

module six_comparator(

input i_rst_n,

         i_start,  

input signed[`EXTENDWORDSIZE:0] i_t_1st_max,

                                                      i_t_2nd_max,

                                                     i_t_3th_max,

                                                      i_b_1st_max,

                                                      i_b_2nd_max,

                                                      i_b_3th_max,

input [`INDEX:0i_Num_t_1st_max,

                        i_Num_t_2nd_max,

                       i_Num_t_3th_max,

                       i_Num_b_1st_max,

                       i_Num_b_2nd_max,

                       i_Num_b_3th_max,

output [`INDEX:0] o_index_1stmax,

                           o_index_2ndmax,

                           o_index_3thmax,

output signed[`EXTENDWORDSIZE:0] o_all_1st_max,

                                                        o_all_2nd_max,

                                                        o_all_3th_max

                            );

       wire signed[`EXTENDWORDSIZE:0] tt_1st_max, tt_2nd_max,tt_3th_max, bb_1st_max, bb_2nd_max,bb_3th_max;

       wire[`INDEX:0] Num_tt_1st_max, Num_tt_2nd_max, Num_tt_3th_max, Num_bb_1st_max, Num_bb_2nd_max,Num_bb_3th_max;

 

   rank3data top(

  .i_rst_n(i_rst_n),

  .i_start(i_start),

  .i_t_1st_max(i_t_1st_max),

  .i_t_2nd_max(i_t_2nd_max),

  .i_t_3th_max(i_t_3th_max),

  .i_Num_t_1st_max(i_Num_t_1st_max),

  .i_Num_t_2nd_max(i_Num_t_2nd_max),

  .i_Num_t_3th_max(i_Num_t_3th_max),

  .o_index_1stmax(Num_tt_1st_max),

  .o_index_2ndmax(Num_tt_2nd_max),

  .o_index_3thmax(Num_tt_3th_max),

  .o_all_1st_max(tt_1st_max),

  .o_all_2nd_max(tt_2nd_max),

  .o_all_3th_max(tt_3th_max)

          );

 rank3data bottom(

    .i_rst_n(i_rst_n),

   .i_start(i_start),

    .i_t_1st_max(i_b_1st_max),

   .i_t_2nd_max(i_b_2nd_max),

   .i_t_3th_max(i_b_3th_max),

  .i_Num_t_1st_max(i_Num_b_1st_max),

  .i_Num_t_2nd_max(i_Num_b_2nd_max),

  .i_Num_t_3th_max(i_Num_b_3th_max),

 .o_index_1stmax(Num_bb_1st_max),

 .o_index_2ndmax(Num_bb_2nd_max),

 .o_index_3thmax(Num_bb_3th_max),

  .o_all_1st_max(bb_1st_max),

  .o_all_2nd_max(bb_2nd_max),

  .o_all_3th_max(bb_3th_max)

          );

   two3rank_compare findMAX(

          .i_rst_n(i_rst_n),

          .i_start(i_start),

         .i_t_1st_max(tt_1st_max),

          .i_t_2nd_max(tt_2nd_max),

          .i_t_3th_max(tt_3th_max),

          .i_b_1st_max(bb_1st_max),

          .i_b_2nd_max(bb_2nd_max),

          .i_b_3th_max(bb_3th_max),

          .i_Num_t_1st_max(Num_tt_1st_max),

          .i_Num_t_2nd_max(Num_tt_2nd_max),

           .i_Num_t_3th_max(Num_tt_3th_max),

           .i_Num_b_1st_max(Num_bb_1st_max),

           .i_Num_b_2nd_max(Num_bb_2nd_max),

           .i_Num_b_3th_max(Num_bb_3th_max),

          .o_index_1stmax(o_index_1stmax),

          .o_index_2ndmax(o_index_2ndmax),

           .o_index_3thmax(o_index_3thmax),

          .o_all_1st_max(o_all_1st_max),

           .o_all_2nd_max(o_all_2nd_max),

           .o_all_3th_max(o_all_3th_max)

                            );

endmodule


matlab code


function  softMetricout2=Nord_Ronbin_Decoder_MST_43(pvecMetric_in,Integer_bit,Decimal_bit)
%==========================================================================================
%  Project Code        : DTMB Link Level Simulation
%  Create Date         : 2014/04/09
%  Description         : Nordstrom-Robinson decoder, used in the 4QAM-NR demapping of DTMB
%  Version             : V0.1
%  Modification History:
%====================================================================

%%%------------------------parameters------------------------------%%%
format longG;
global S
pvecMetric11=pvecMetric_in(:,3)';  
% Quantification :8bit wide = 1bit Symbol + 4bit Integer + 3bit Decimal
demapperout2=[];
for t=1:1:7488
    demapperout2(t)=floor(pvecMetric11(1,t)*2^(Decimal_bit));
    if(demapperout2(t)>(2^(Integer_bit+Decimal_bit)-1)) demapperout2(t)=2^(Integer_bit+Decimal_bit)-1;
    else if(demapperout2(t)<=-(2^(Integer_bit+Decimal_bit))) demapperout2(t)=-(2^(Integer_bit+Decimal_bit));
        end
    end
end
pvecMetric=demapperout2;

y=zeros(1,8);
softMetric_out=zeros(1,3744);
NR_decoder_output=zeros(1,3744);
data_length=length(pvecMetric);

 fp=fopen('input7488_611_mat.txt','w');
    for i=1:7488
       fprintf(fp,'%d ',pvecMetric(i));
    end
 fclose(fp);
%%%------------------------calculate the 256 states----------------------------------------%%%
% for i=1:1:256       
%         x=fliplr(de2bi(i-1,8));
%         x7=x(1); 
%         x6=x(2); 
%         x5=x(3); 
%         x4=x(4);
%         x3=x(5); 
%         x2=x(6); 
%         x1=x(7); 
%         x0=x(8);
%         y(1) = mod(x7+x6+x0+x1+x3+(xor(x0,x4)&(mod(x1+x2+x3+x5,2)))+(xor(x1,x2)&(xor(x3,x5))),2);
%         y(2) = mod(x7+x0+x1+x2+x4+(xor(x1,x5)&(mod(x2+x3+x4+x6,2)))+(xor(x2,x3)&(xor(x4,x6))),2); 
%         y(3) = mod(x7+x1+x2+x3+x5+(xor(x2,x6)&(mod(x3+x4+x5+x0,2)))+(xor(x3,x4)&(xor(x5,x0))),2);
%         y(4) = mod(x7+x2+x3+x4+x6+(xor(x3,x0)&(mod(x4+x5+x6+x1,2)))+(xor(x4,x5)&(xor(x6,x1))),2);
%         y(5) = mod(x7+x3+x4+x5+x0+(xor(x4,x1)&(mod(x5+x6+x0+x2,2)))+(xor(x5,x6)&(xor(x0,x2))),2);
%         y(6) = mod(x7+x4+x5+x6+x1+(xor(x5,x2)&(mod(x6+x0+x1+x3,2)))+(xor(x6,x0)&(xor(x1,x3))),2);
%         y(7) = mod(x7+x5+x6+x0+x2+(xor(x6,x3)&(mod(x0+x1+x2+x4,2)))+(xor(x0,x1)&(xor(x2,x4))),2);
%         y(8) = mod((x0+x1+x2+x3+x4+x5+x6+x7+y(1)+y(2)+y(3)+y(4)+y(5)+y(6)+y(7)),2);
%         %calculate 256 states  16bit
%         for j=1:1:8
%           C(i,j)=x(9-j);
%         end
%         for k=1:1:8
%           C(i,8+k)=y(k);
%         end
%        
%         %s=1-2c
%         for m=1:1:16
%             S(i,m)=1-2*C(i,m);
%         end        
% end
% load C;
% load S;

%%%-------------Find the maximum correlation rm, the sub-maximum correlation rsm----------------%%%
for i=0:16:(data_length-1)
     % calculate tne rc
    rc=zeros(1,256);
    for state_num=1:1:256
        for j=1:1:16
%            if ((S(state_num,j)==-1)&(pvecMetric(i+j)==-128))
%              rc(state_num)=rc(state_num)+ 127;
%            else
%            rc(state_num)=rc(state_num)+ S(state_num,j)*pvecMetric(i+j);
%            en
            if (S(state_num,j)==-1)
                if(pvecMetric(i+j)== -128)
                  rc(state_num)=rc(state_num)+ 127; 
                else
                   rc(state_num)=rc(state_num)-pvecMetric(i+j);  
                end
            else
             rc(state_num)=rc(state_num)+pvecMetric(i+j);  
            end
               
        end
    end
%     pvecMetric(1,1:16)
%     save rc;
%    
%    fp=fopen('rc_256state.txt','w');
%     for i=1:256
%        fprintf(fp,'%d \n',rc(i));
%     end
%     fclose(fp);
    %
    max_rc=min(rc);%
    smax_rc=min(rc);
    tmax_rc=min(rc);
    C_max=zeros(1,16);
    C_smax=zeros(1,16);
    C_tmax=zeros(1,16);
    max_pos=0;
    smax_pos=0;
 
     %Find the maximum correlation max_rc and its corresponding codewords
    for t=1:1:256
        if(rc(t)>max_rc)
            max_rc=rc(t);
            C_max(1,:)=S(t,:);
            max_pos=t;
        end
    end
   
    %Find the sub-maximum correlation smax_rc and its corresponding codewords
    for l=1:1:256
      if l~=max_pos  %skip the maximum piont
        if(rc(l)>smax_rc)
            smax_rc=rc(l);
            C_smax(1,:)=S(l,:);
            smax_pos=l;
        end
      end
    end
   
      %Find the Third Maximum  correlation tmax_rc and its corresponding codewords
    for ll=1:1:256
      if ll~=max_pos  %skip the maximum piont
          if ll~=smax_pos %skip the sub-maximum piont
           if(rc(ll)>tmax_rc)
            tmax_rc=rc(ll);
            C_tmax(1,:)=S(ll,:);
           end
          end
      end
    end
     
 %%%-------------calculate the output LLR----------------%%%        
    for num=1:8
        %For those information bits different between C_max  C_smax
        if(C_smax(num)~=C_max(num))
              softMetric_out(floor(i/2)+num)= (1/2)*((C_max(num)*max_rc)-(C_max(num)*smax_rc));
        else  if(C_tmax(num)~=C_max(num)) %for the same bits within  C_max and C_smax, but different from C_tmax
               softMetric_out(floor(i/2)+num)= (1/2)*((C_max(num)*max_rc)- (C_max(num)*tmax_rc));
             else    % for the same bits within  C_max,  C_smax and C_tmax
               softMetric_out(floor(i/2)+num)= (1/2)*((C_max(num)*max_rc)- 0.875*(C_max(num)*tmax_rc));
              end
        end
    end
%     softMetric_out(1:8)
end

for t=1:1:3744
    if ((softMetric_out(1,t)>0))
    softMetricout2(t)=floor(softMetric_out(1,t)); 
    else
    softMetricout2(t)=round(softMetric_out(1,t));
    end
%   
%     else
%     end
    if(softMetricout2(t)>(2^(Integer_bit+Decimal_bit)-1)) softMetricout2(t)=2^(Integer_bit+Decimal_bit)-1;
    else if(softMetricout2(t)<=-(2^(Integer_bit+Decimal_bit))) softMetricout2(t)=-(2^(Integer_bit+Decimal_bit));
        end
    end
end

end
   
       
   


 Verilog 找出任意六个数中的最大、次最大和第三最大值以及三个数对应的序号_第1张图片





你可能感兴趣的:(FPGA,verilog,六个数找最大次最大和第三最大,三个数排序)