⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsp.h

📁 dsPIC33F单片机的ADC采样以及IIR数字滤波的应用范例代码
💻 H
📖 第 1 页 / 共 4 页
字号:
* in Y-Data memory.
*
****************************************************************************/

/* FIR filter operation prototypes. */


typedef struct {
   int numCoeffs;			/* number of coeffs in filter (M) */
					/* (same as filter order if lattice) */
   					/* (h[m], 0 <= m < M) */
   					/* (if lattice, k[m], 0 <= m < M) */
   fractional* coeffsBase;		/* base address of filter coeffs */
					/* either in X data or program memory */
					/* if in X data memory, it points at */
   					/* h[0] (if lattice, k[0]) */
					/* if in program memory, base is the */
					/* offset from program page boundary */
					/* to address where coeffs located */
					/* (inline assembly psvoffset ()) */
					/* when indicated, it must be at a */
					/* 'zero' power of 2 address (since */
					/* in those cases it is implemented */
					/* as an increasing circular buffer) */
   fractional* coeffsEnd;		/* end address of filter coeffs */
					/* must be an odd number */
					/* if in data memory, points at */
					/* last byte of coefficients buffer */
					/* if in program memory, end is the */
					/* offset from program page boundary */
					/* to address of last byte of coeffs */
   int coeffsPage;			/* if in X data memory, set to */
   					/* defined value COEFFS_IN_DATA */
					/* if in program memory, page number */
					/* where coeffs are located */
					/* (inline assembly psvpage ()) */
   fractional* delayBase;		/* base address of delay buffer, */
					/* only in Y data */
   					/* points at d[0] of d[m], 0 <= m < M */
					/* when indicated, it must be at a */
					/* 'zero' power of 2 address (since */
					/* in those cases it is implemented */
					/* as an increasing circular buffer) */
   fractional* delayEnd;		/* end address of delay buffer, */
   					/* points at last byte of buffer */
   fractional* delay;			/* current value of delay pointer */
} FIRStruct;


extern void FIRStructInit (		/* Initialize FIR filter structure */
   FIRStruct* FIRFilter,		/* FIR filter structure */
   int numCoeffs,			/* number of coeffs in filter (M) */
					/* (same as filter order if lattice) */
   					/* (h[m], 0 <= m < M) */
   					/* (if lattice, k[m], 0 <= m < M) */
   fractional* coeffsBase,		/* base address of filter coeffs */
					/* either in X data or program memory */
					/* if in X data memory, it points at */
   					/* h[0] (if lattice, k[0]) */
					/* if in program memory, base is the */
					/* offset from program page boundary */
					/* to address where coeffs located */
					/* (inline assembly psvoffset ()) */
					/* when indicated, it must be at a */
					/* 'zero' power of 2 address (since */
					/* in those cases it is implemented */
					/* as an increasing circular buffer) */
   int coeffsPage,			/* if in X data memory, set to */
   					/* defined value COEFFS_IN_DATA */
					/* if in program memory, page number */
					/* where coeffs are located */
					/* (inline assembly psvpage ()) */
   fractional* delayBase		/* base address of delay buffer, */
					/* only in Y data */
   					/* points at d[0] of d[m], 0 <= m < M */
					/* when indicated, it must be at a */
					/* 'zero' power of 2 address (since */
					/* in those cases it is implemented */
					/* as an increasing circular buffer) */
					/* upon return, FIR filter structure */
					/* is initialized (delay = delayBase) */
);


extern void FIRDelayInit (		/* Zero out dealy in filter structure */
   FIRStruct* filter			/* filter structure */
   					/* NOTE: FIR interpolator's delay is */
					/* initialized by FIRInterpDelayInit */
);


extern void FIRInterpDelayInit (	/* Zero out dealy in filter structure */
					/* for FIR interpolator */
   FIRStruct* filter,			/* filter structure */
   int rate				/* rate of interpolation (1 to) R */
);


extern fractional* FIR (		/* FIR filtering */
   int numSamps,			/* number of input samples (N) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N) */
   FIRStruct* filter			/* filter structure: */
   					/* number of coefficients in filter */
   					/* same as number of delay elements */
   					/* (h[m], 0 <= m < M, an increasing */
					/*  circular buffer) */
   					/* (d[m], 0 <= m < M, an increasing */
					/*  circular buffer) */

   					/* returns dstSamps */
);


extern fractional* FIRDecimate (	/* Decimation by R:1 rate */
   int numSamps,			/* number of _output_ samples (N) */
   					/* N = R*p (p integer) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N*R) */
   FIRStruct* filter,			/* filter structure: */
   					/* number of coefficients in filter */
   					/* same as number of delay elements */
   					/* M = R*q (q integer) */
   					/* (h[m], 0 <= m < M) */
   					/* (d[m], 0 <= m < M) */
   int rate				/* rate of decimation R (to 1) */

   					/* returns dstSamps */
);


extern fractional* FIRInterpolate (	/* Interpolation by 1:R rate */
   int numSamps,			/* number of input samples (N) */
   					/* N = R*p (p integer) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N*R) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N) */
   FIRStruct* filter,			/* filter structure: */
   					/* number of coefficients in filter */
   					/* M = R*q (q integer) */
   					/* (h[m], 0 <= m < M) */
   					/* (d[m], 0 <= m < M/R) */
   int rate				/* rate of interpolation (1 to) R */

   					/* returns dstSamps */
);


extern fractional* FIRLattice (		/* FIR Lattice filtering */
   int numSamps,			/* number of input samples (N) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N) */
   FIRStruct* filter			/* filter structure: */
   					/* number of coefficients in filter */
   					/* (also known as kappa values) */
   					/* same as number of delay elements */
   					/* same as filter order */
   					/* (k[m], 0 <= m < M) */
   					/* (d[m], 0 <= m < M) */

   					/* returns dstSamps */
);

extern fractional* FIRLMS (		/* FIR Least Mean Square filtering */
					/* not normalized implementation */
					/* y[n] = sum_{m=0:M-1){h[n]*x[n-m]} */
					/* 0 <= n < N */
					/* h_m[n] = h_m[n-1] + mu*e[n]*x[n-m] */
					/* 0 <= n < N, 0 <= m < M */
					/* with e[n] = r[n] - y[n] */
					/* NOTE: to avoid saturation while */
					/* computing error, -1 <= e[n] < 1 */
					/* for 0 <= n < N */
   int numSamps,			/* number of input samples (N) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N) */
   FIRStruct* filter,			/* filter structure: */
   					/* number of coefficients in filter */
   					/* same as number of delay elements */
   					/* (h[m], 0 <= m < M, an increasing */
					/*  circular buffer) */
   					/* (d[m], 0 <= m < M, an increasing */
					/*  circular buffer) */
   fractional* refSamps,		/* ptr to reference samples */
   					/* (r[n], 0 <= n < N) */
   fractional muVal			/* mu value for correction */

   					/* returns dstSamps */
);

extern fractional* FIRLMSNorm (		/* FIR Least Mean Square filtering*/
					/* Normalized implementation */
					/* y[n] = sum_{m=0:M-1}(h[n]*x[n-m]) */
					/* 0 <= n < N */
					/* h_m[n] = h_m[n-1] + nu*e[n]*x[n-m] */
					/* 0 <= n < N, 0 <= m < M */
					/* with e[n] = r[n] - y[n], and */
					/* nu[n] = mu/(mu+E[n]) */
					/* E[n]=E[n-1]+(x[n])^2-(x[n-M+1])^2 */
					/* is an estimate of input energy */
					/* NOTE: to avoid saturation while */
					/* computing error, -1 <= e[n] < 1 */
					/* for 0 <= n < N */
					/* NOTE: to avoid saturation while */
					/* computing the estimate, the input */
					/* signal values should be bound */
					/* so that sum_{m=0:-M+2}(x[n+m]^2}<1 */
   int numSamps,			/* number of input samples (N) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N) */
   FIRStruct* filter,			/* filter structure: */
   					/* number of coefficients in filter */
   					/* same as number of delay elements */
   					/* (h[m], 0 <= m < M, an increasing */
					/*  circular buffer) */
   					/* (d[m], 0 <= m < M, an increasing */
					/*  circular buffer) */
   fractional* refSamps,		/* ptr to reference samples */
   					/* (r[n], 0 <= n < N) */
   fractional muVal,			/* mu value for correction */
   fractional* energyEstimate		/* energy estimate for input samples */
					/* E[-1] = (x[-1])^2+...+(x[-M+1])^2 */
					/* on start up... (zero first time) */
					/* E[N-1]=(x[N-1])^2+...+(x[N-M+1])^2 */
   					/* upon return */

   					/* returns dstSamps */
);

/* ....................................................................... */

/****************************************************************************
*
* Interface to IIR filter operations.
*
* Filtering a data sequence x[n] with an IIR filter of impulse response
* {b[m] (0<= m < M), a[p] (0 <= p < P)} is equivalent to solving the
* difference equation:
*
*	sum_{p = 0:P-1}(a[p]*y[n-p]) = sum_{m = 0:M-1}(b[m]*x[n-m])
*
* In this operation it is important to know and manage the past history
* of the input and output data sequences (x[m], -M+1 <= m < 0, and y[p],
* -P+1 <= p < 0) which represent the initial conditions of the filtering
* operation. Also, when applying an IIR filter to contiguous sections of
* a data sequence it is necessary to remember the final state of the
* last filtering operation (x[m], N-M+1 <= m < N-1, and y[p], N-P+1 <= p < N-1),
* and take the state into consideration for the calculations of the next
* filtering stage. Accounting for the past history and current state is
* required to perform a correct (glitch-free) filtering operation.
*
* The management of the past history and current state of the filtering
* operation is commonly implemented via additional sequences, referred
* to as the delays. Prior to a filtering operation the delays describe the
* past history of the filter. After performing the IIR filtering operation
* the delays contain a set of the most recently filtered data samples, and
* of the most recent output samples. (For correct operation, it is advisable
* to initialize the delay values to zero by calling the corresponding init
* function.)
*
* Even though IIR filtering is a difference equation, several properties
* of IIR filters allow for computation of the operation in more effective
* ways than that of a straight difference equation. Consequently, a set
* of such implementations are hereby provided.
*
* Note that of the six main sequences involved in IIR filtering, input
* data, output data, filter coefficients (a,b) and delays, the last four
* are usually thought of as making up the filter structure. However, since
* different implementations allow for particular arrangaments of the filter
* structure for efficiency gain, the structure has not been standardized,
* but rather taylored to best fit the particular implementations.
*
* In the current design, the input data sequence is referred to as the
* sequence of source samples, while the resulting filtered sequence
* contains the destination samples. The filters are characterized by
* the number of coefficients or taps in the 'a' and 'b' sets, and the
* delay properties. All of these data sets are stored in memory as
* vectors with their elements representing Q.15 fractional quantities.
* Also, except for the 'IIRLattice' and associated 'IIRLatticeStruct'
* implementations, the filters are made up of cascaded second order
* sections. In all cases, the input, output and coefficient vectors
* are allocated in default RAM memory space (X-Data and Y-Data). The
* coefficients may be allocated in either X-Data or program memory,
* while the delays ought to be in Y-Data.
*
****************************************************************************/

/* IIR filter operation prototypes. */


typedef struct {
  int numSectionsLess1;			/* 1 less than number of cascaded */
  					/* second order sections */
  fractional* coeffsBase;		/* ptr to filter coefficients */
					/* either in X-Data or P-MEM */
					/* number of coefficients is */
					/* 5*number of second order sections */
					/* {a2,a1,b2,b1,b0} per section */
  int coeffsPage;			/* page number of program memory if */
					/* coefficients are in program memory */
					/* COEFFS_IN_DATA if not */
  fractional* delayBase;		/* ptr to filter delay */
					/* two words for every section */
					/* only in Y-Data */
  fractional initialGain;		/* initial gain value */
  int finalShift;			/* output scaling (shift left) */
					/* restores filter gain to 0 dB */
					/* shift count may be zero, if not */
					/* zero, it is the number of bits */
					/* to shift output: negative means */
					/* shift to the left, positive is */
					/* shift right */
} IIRCanonicStruct;	/* Direct Form II Canonic biquad filter structure */

extern fractional* IIRCanonic (		/* Direct Form II (Canonic) */
					/* biquad filtering */
   int numSamps,			/* number of input samples (N) */
   fractional* dstSamps,		/* ptr to output samples */
   					/* (y[n], 0 <= n < N) */
   fractional* srcSamps,		/* ptr to input samples */
   					/* (x[n], 0 <= n < N) */
   IIRCanonicStruct* filter		/* filter structure */

   					/* returns dstSamps */
);

extern void IIRCanonicInit (  		/* Initialize filter structure */
   IIRCanonicStruct* filter		/* Canonic biquad filter structure */
);

/* ....................................................................... */

typedef struct {
  int numSectionsLess1;			/* 1 less than number of cascaded */
  					/* second order sections */
  fractional* coeffsBase;		/* ptr to filter coefficients */
					/* either in X-Data or P-MEM */
					/* number of coefficients is */
					/* 5*number of second order sections */
					/* {b0,b1,a1,b2,a2} per section */
  int coeffsPage;			/* page number of program memory if */
					/* coefficients are in program memory */
					/* COEFFS_IN_DATA if not */
  fractional* delayBase1;		/* ptr to state variable (delay) 1 */
					/* one word for every section */
					/* only in Y-Data */
  fractional* delayBase2;		/* ptr to state variable (delay) 2 */
					/* one word for every section */
					/* only in Y-Data */
  int finalShift;			/* output scaling (shift left) */
					/* restores filter gain to 0 dB */
					/* shift count may be zero, if not */
					/* zero, it is the number of bits */
					/* to shift the output to the left */
					/* negative value means shift right */
} IIRTransposedStruct;	/* Transposed Direct Form II biquad filter structure */

extern fractional* IIRTransposed (	/* Direct Form II (Transposed) */
					/* biquad filtering */
   int numSamps,			/* number of input samples (N) */
   fractional* dstSamps,		/* ptr to output samples */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -