-- recursive averaging library IEEE; use IEEE.std_logic_1164.all; use ieee.numeric_std.all; entity reg1 is port ( clk : in std_logic; reset : in std_logic; start_en : in std_logic; data_in : in std_logic_vector(11 downto 0); --current sample in from SINC filter u_h : in std_logic; v_h : in std_logic; w_h : in std_logic; avg_out : out signed (18 downto 0) ); end reg1; architecture RTL of reg1 is signal current_avg_old :signed (18 downto 0); signal time_avg_old :signed (15 downto 0); signal time_sqr_avg_old :signed (31 downto 0); signal current_time_avg_old :signed (34 downto 0); begin process(data_in) variable diff :signed(18 downto 0); variable current_avg :signed(18 downto 0); variable scaled_current :signed(18 downto 0); variable count :integer range 0 to 53;--signed(8 downto 0); variable resize_current :signed(18 downto 0); begin if ( reset='0' ) then current_avg_old <=(others =>'0'); diff :=(others =>'0'); current_avg :=(others =>'0'); scaled_current :=(others =>'0'); count :=0;--(others =>'0'); -- state <= 0; elsif (rising_edge(clk)) then if (start_en='1') then --PWM Block up_down_out if (not(u_h='1' and v_h='1' and w_h='1') or (u_h='0' and v_h='0' and w_h='0')) then -- start regression count :=count+1; resize_current:= resize(signed(data_in),19); scaled_current:= resize_current sll 7; diff := signed(scaled_current(18 downto 0)) - signed(current_avg_old(18 downto 0)); current_avg:= current_avg_old + (diff/count); current_avg_old<=current_avg; else count :=0; end if; avg_out<=current_avg; end if; end if; end process; end RTL;