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

📄 parametric_equalizertest.c

📁 CHP 5 - Real-Time Digital Signal Processing: Implementations and Applications, Second Edition by Sen
💻 C
字号:
// 
//  Project: Experiment 5.7.7: implementation of parametric equalizer using IIR filter - Chapter 5
//  File name: parametric_equalizerTest.c   
//
//  Description: This is the test program for parametric equalizer using IIR filter 
//
//  For the book "Real Time Digital Signal Processing: 
//                Implementation and Application, 2nd Ed"
//                By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
//                Publisher: John Wiley and Sons, Ltd
//
//  Tools used: CCS v.2.12.07
//              TMS320VC5510 DSK Rev-C
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "cascadeIIR.h"

#define SECTIONS    2  // Number of 2nd order sections
short   C[SECTIONS*5]; // Filter coefficients obtained from example 5.14 MATLAB FDATool
                       // C[]=A[i][1], A[i][2], B[i][2], B[i][0], B[i][1]... 
short   w[SECTIONS*2]; // Filter delay line
                       // w[]=w[i][n-1],w[i+1][n-1],...,w[i][n-2],w[i+1][n-2],...

#define NUM_DATA  128  // Number of samples per block 
short   out[NUM_DATA]; // Filter output data buffer
short   in[NUM_DATA];  // Filter input data buffer

double gain200[13][2]={ // Parameter at 200 Hz 
	// rz     rp
	{0.8900, 0.8000},  // -6 dB
	{0.8775, 0.8000},  // -5 dB
	{0.8640, 0.8000},  // -4 dB
	{0.8495, 0.8000},  // -3 dB
	{0.8340, 0.8000},  // -2 dB
	{0.8174, 0.8000},  // -1 dB
	{0.8000, 0.8000},  // 0 dB
	{0.8000, 0.8174},  // 1 dB
	{0.8000, 0.8340},  // 2 dB
	{0.8000, 0.8495},  // 3 dB
	{0.8000, 0.8640},  // 4 dB
	{0.8000, 0.8775},  // 5 dB
	{0.8000, 0.8900}}; // 6 dB

double gain1000[13][2]={ // Parameter at 1000 Hz
	// rz     rp
	{0.9048, 0.8000},  // -6 dB
	{0.8925, 0.8000},  // -5 dB
	{0.8784, 0.8000},  // -4 dB
	{0.8627, 0.8000},  // -3 dB
	{0.8445, 0.8000},  // -2 dB
	{0.8238, 0.8000},  // -1 dB
	{0.8000, 0.8000},  // 0 dB
	{0.8000, 0.8238},  // 1 dB
	{0.8000, 0.8445},  // 2 dB
	{0.8000, 0.8627},  // 3 dB
	{0.8000, 0.8784},  // 4 dB
	{0.8000, 0.8925},  // 5 dB
	{0.8000, 0.9048}}; // 6 dB

enum {
	NEG_6dB=0,NEG_5dB,NEG_4dB,NEG_3dB,NEG_2dB,NEG_1dB,
	ZERO_dB,POS_1dB,POS_2dB,POS_3dB,POS_4dB,POS_5dB,POS_6dB
};

    
static void coefGen(double (*gainTbl)[2], short gain, short *c, float freq);

void main(void)
{
  FILE *fpIn,*fpOut;
  short i,k;
  char  temp[NUM_DATA*2];

  fpIn = fopen("..\\data\\input.pcm", "rb");
  fpOut = fopen("..\\data\\output.pcm", "wb");

  if (fpIn == NULL)
  {
    printf("Can't open input PCM file\n");
    exit(0);
  }

  // Generate filter coefficients for high band
  coefGen(gain1000, NEG_6dB, &C[0], 1000);
	
  // Generate filter coefficients for low band
  coefGen(gain200, POS_6dB, &C[5], 200);

  // Initialize IIR filter delay line
  for (i=0; i<2*SECTIONS;i++)
  {
    w[i] = 0; 
  }

  // Start equalizer test
  while (fread(temp, sizeof(char), NUM_DATA*2, fpIn) == (NUM_DATA*2))
  {
    for (k=0, i=0; i<NUM_DATA; i++)
    {
      in[i] = (temp[k]&0xFF)|(temp[k+1]<<8);
      k += 2;
    }

    cascadeIIR(in,NUM_DATA,out,C,SECTIONS,w); // Filter a block of samples  

    for (k=0, i=0; i<NUM_DATA; i++)
    {
      temp[k++] = (out[i]&0xFF);
      temp[k++] = (out[i]>>8)&0xFF;
    }
    fwrite(temp, sizeof(char), NUM_DATA*2, fpOut);
  }
  fclose(fpOut);	
  fclose(fpIn);
}


// Generate coefficient using the following equations
//  b = [1, -2*rz*cos(w), rz*rz];
//  a = [1, -2*rp*cos(w), rp*rp];
//  w = 2*PI*freq/F: F = 8000Hz

static void coefGen(double (*gainTbl)[2], short gain, short *c, float freq)
{
  double rz,rp,temp,omega;

  rz = gainTbl[gain][0];              // Get rz from the lookup table
  rp = gainTbl[gain][1];              // Get rp from the lookup table

  omega = 2.0*3.1415926*freq/8000.0;  // This example uses 8kHz sampling rate

  c[3] = 0x4000;                      // b[0] Q14 coefficient
  temp = -2.0*rz*cos(omega);
  c[4] = (short)(temp*16384.0+0.5);   // b[1]
  c[2] = (short)(rz*rz*16384.0+0.5);  // b[2]

  temp = -2.0*rp*cos(omega);
  c[0] = (short)(temp*16384.0+0.5);   // a[1]
  c[1] = (short)(rp*rp*16384.0+0.5);  // a[2]
}

⌨️ 快捷键说明

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