view src/samer/units/IIRFilter.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.units;
import  samer.core.*;
import  samer.maths.*;
import  samer.tools.*;
import  java.util.*;

public class IIRFilter implements Filter
{
	int		N;		// order of filter
	double	A[];	// coefficients
	double	u[];	// circular buffer of previous values
	int		k;    // current position in u buffer

	public IIRFilter( int n)
	{
		N=n; 
		u=new double[n];
		A=new double[n+1];
		reset();
	}

	public IIRFilter( double [] A)
	{
		N=A.length-1; 
		u=new double[N];
		this.A=A;
		reset();
	}

	public void dispose() {}
	public void reset() { Mathx.zero(u); k=0; }

	/** FIR filter using array of filter coefficients */
	public final double filter( double z)
	{
		double	y=z*A[0]; 

		int i=1, j;
		for (j=k; j<N; j++) y += u[j]*A[i++];
		for (j=0; j<k; j++) y += u[j]*A[i++];

		if (k==0) k=N-1; else k--;
		u[k]=y; // store output value in buffer
		return y;
	}
}