📄 firdemo.c
字号:
if ((up2_ptr = hq_up_1_to_2_init()) == 0) HARAKIRI("hq_up_1_to_2 initialization failure!\n", 1); } else if (up_2 == -2) { /* get pointer to a struct which contains bpf coefficients and cleared * state variables for a upsampling factor of 2 */ if ((up2_ptr = linear_phase_pb_1_to_2_init()) == 0) HARAKIRI("linear-phase band-pass 1:2 initialization failure!\n", 1); } else if (up_2 == 3) { /* get pointer to a struct which contains filter coefficients and cleared * state variables for a upsampling factor of 3 */ if ((up2_ptr = hq_up_1_to_3_init()) == 0) HARAKIRI("hq_up_1_to_3 initialization failure!\n", 1); } else up2_ptr = NULL; /* First Downsampling Procedure */ if (down_1 == 2) { /* get pointer to a struct which contains filter coefficients and cleared * state variables for a downsampling factor of 2 */ if ((down1_ptr = hq_down_2_to_1_init()) == 0) HARAKIRI("hq_down_2_to_1 initialization failure!\n", 1); } else if (down_1 == -2) { /* get pointer to a struct which contains bpf coefficients and cleared * state variables for a downsampling factor of 2 */ if ((down1_ptr = linear_phase_pb_2_to_1_init()) == 0) HARAKIRI("linear-phase band-pass 2:1 initialization failure!\n", 1); } else if (down_1 == 3) { /* get pointer to a struct which contains filter coefficients and cleared * state variables for a downsampling factor of 3 */ if ((down1_ptr = hq_down_3_to_1_init()) == 0) HARAKIRI("hq_down_3_to_1 initialization failure!\n", 1); } else down1_ptr = NULL; /* Second Downsampling Procedure */ if (down_2 == 2) { /* get pointer to a struct which contains filter coefficients and cleared * state variables for a downsampling factor of 2 */ if ((down2_ptr = hq_down_2_to_1_init()) == 0) HARAKIRI("hq_down_2_to_1 initialization failure!\n", 1); } else if (down_2 == -2) { /* get pointer to a struct which contains bpf coefficients and cleared * state variables for a downsampling factor of 2 */ if ((down2_ptr = linear_phase_pb_2_to_1_init()) == 0) HARAKIRI("linear-phase band-pass 2:1 initialization failure!\n", 1); } else if (down_2 == 3) { /* get pointer to a struct which contains filter coefficients and cleared * state variables for a downsampling factor of 3 */ if ((down2_ptr = hq_down_3_to_1_init()) == 0) HARAKIRI("hq_down_3_to_1 initialization failure!\n", 1); } else down2_ptr = NULL;/* * ... Print some infos ... */ if (modified_irs) fprintf(stderr, "Running modified IRS\n"); if (half_tilt) fprintf(stderr, "Running half-tilt IRS\n"); if (down_1<0 || down_2<0 || up_1<0 || up_2 <0) fprintf(stderr, "Running flat FIR band-pass filter\n");/* * ......... CARRY OUT FILTERING ......... */ /* Start measuring CPU-time -- includes file I/O! */ t1 = clock(); lsegx = lseg; while (lsegx == lseg) { /* Read data from file in a short array ... */ lsegx = fread(sh_buff, sizeof(short), lseg, inpfilptr); /* ... and convert short to float, normalizing */ sh2fl_16bit(lsegx, sh_buff, fl_buff, 1); /* 1ST STAGE: which kind of IRS filtering? (0=none / 8=8kHz / 16=16kHz) */ if (irs == 0) { /* copy input buffer to 1st stage output (irs buffer) */ for (k = 0; k <= lsegx - 1; k++) irs_buff[k] = fl_buff[k]; lsegirs = lsegx; /* Returned: number of output samples */ } else { /* Filter using the IRS coeffs. initialized before (8 or 16 kHz) */ lsegirs = /* Returned: number of output samples */ hq_kernel( /* IRS send part filter */ lsegx, /* In : number of input samples */ fl_buff, /* In : array with input samples */ irs_ptr, /* InOut: pointer to FIR struct */ irs_buff /* Out : array with output samples */ ); /* Convert to integer for testing overflows -- do not save! */ noverflows0 += fl2sh_16bit(lsegirs, irs_buff, sh_buff, (int) 1); } /* INTERMEDIATE STAGE: Apply Delta-SM? (0=no / 1=yes) */ if (delta_sm) { /* Copy IRS-processed buffer back onto the original data buffer */ for (k = 0; k < lsegirs; k++) fl_buff[k] = irs_buff[k]; /* Apply filtering */ lsegirs = /* Returned: number of output samples */ hq_kernel( /* IRS send part filter */ lsegx, /* In : number of input samples */ fl_buff, /* In : array with input samples */ delta_sm_ptr, /* InOut: pointer to FIR struct */ irs_buff /* Out : array with output samples */ ); /* Convert to integer for testing overflows -- do not save! */ noverflows1 += fl2sh_16bit(lsegirs, irs_buff, sh_buff, (int) 1); } /* 2ND STAGE: First upsampling filter (0=none / +-2=1:2 / 3=1:3) */ if (up_1 == 0) { /* copy irs buffer to 2nd stage output (up-sample buffer 1) */ for (k = 0; k <= lsegirs - 1; k++) up1_buff[k] = irs_buff[k]; lsegup1 = lsegirs; /* Returned: number of output samples */ } else { lsegup1 = /* Returned: number of output samples */ hq_kernel( /* (up-sampling filter) */ lsegirs, /* In : number of input samples */ irs_buff, /* In : array with input samples */ up1_ptr, /* InOut: pointer to FIR struct */ up1_buff /* Out : array with output samples */ ); /* Convert to integer for testing overflows -- do not save! */ noverflows2 += fl2sh_16bit(lsegup1, up1_buff, sh_buff, (int) 1); } /* THIRD STAGE: Second upsampling filter: (0=none / +-2=1:2 / 3=1:3) */ if (up_2 == 0) { /* copy buffer up-sample buf.1 to 3rd stage output (up-sample buf.2) */ for (k = 0; k <= lsegup1 - 1; k++) up2_buff[k] = up1_buff[k]; lsegup2 = lsegup1; /* Returned: number of output samples */ } else { lsegup2 = /* Returned: number of output samples */ hq_kernel( /* (up-sampling filter) */ lsegup1, /* In : number of input samples */ up1_buff, /* In : array with input samples */ up2_ptr, /* InOut: pointer to FIR struct */ up2_buff /* Out : array with output samples */ ); /* Convert to integer for testing overflows -- do not save! */ noverflows3 += fl2sh_16bit(lsegup2, up2_buff, sh_buff, (int) 1); } /* FOURTH STAGE: First downsampling filter: (0=none / +-2=2:1 / 3=3:1) */ if (down_1 == 0) { /* copy buffer up-sample buf.2 to 4th stage output (down-sample buf.1) */ for (k = 0; k <= lsegup2 - 1; k++) down1_buff[k] = up2_buff[k]; lsegdown1 = lsegup2; /* Returned: number of output samples */ } else { lsegdown1 = /* Returned: number of output samples */ hq_kernel( /* (down-sampling filter) */ lsegup2, /* In : number of input samples */ up2_buff, /* In : array with input samples */ down1_ptr, /* InOut: pointer to FIR struct */ down1_buff /* Out : array with output samples */ ); /* Convert to integer for testing overflows -- do not save! */ noverflows4 += fl2sh_16bit(lsegdown1, down1_buff, sh_buff, (int) 1); } /* FIFTH STAGE: Second downsampling filter: (0=none / +-2=2:1 / 3=3:1) */ if (down_2 == 0) { /* copy buffer down-sample buf.1 to 5th stage output (down-sample * buf.2) */ for (k = 0; k <= lsegdown1 - 1; k++) down2_buff[k] = down1_buff[k]; lsegdown2 = lsegdown1; /* Returned: number of output samples */ } else { lsegdown2 = /* Returned: number of output samples */ hq_kernel( /* (down-sampling filter) */ lsegdown1, /* In : number of input samples */ down1_buff, /* In : array with input samples */ down2_ptr, /* InOut: pointer to FIR struct */ down2_buff /* Out : array with output samples */ ); }/* * ......... CONVERTION FROM FLOAT TO SHORT with rounding ......... * (now saves the data!) */ noverflows5 += fl2sh_16bit(lsegdown2, down2_buff, sh_buff, (int) 1);/* * ......... WRITE SAMPLES TO OUTPUT FILE ......... */ nsam += fwrite(sh_buff, sizeof(short), lsegdown2, outfilptr); }/* * ......... FINALIZATIONS ......... */ /* Print time statistics - Include file I/O! */ t2 = clock(); printf("\nDONE: %f sec CPU-time for %ld generated samples\n", (t2 - t1) / (double) CLOCKS_PER_SEC, nsam); /* Print overflow statistics */ if (noverflows0 == 0 && noverflows1 == 0 && noverflows2 == 0 && noverflows3 == 0 && noverflows4 == 0 && noverflows5 == 0) printf(" no overflows occurred\n"); else { printf("\tOverflows - IRS filter ................: %ld\n", noverflows0); printf("\t - Delta-SM filter ...........: %ld\n", noverflows1); printf("\t - up-sampling filter 1 ......: %ld\n", noverflows2); printf("\t - up-sampling filter 2 ......: %ld\n", noverflows3); printf("\t - down-sampling filter 1 ....: %ld\n", noverflows4); printf("\t - down-sampling filter 2 ....: %ld\n", noverflows5); } /* Release allocated FIR structures */ if (down2_ptr != (SCD_FIR *) NULL) hq_free(down2_ptr); if (down1_ptr != (SCD_FIR *) NULL) hq_free(down1_ptr); if (up2_ptr != (SCD_FIR *) NULL) hq_free(up2_ptr); if (up1_ptr != (SCD_FIR *) NULL) hq_free(up1_ptr); if (irs_ptr != (SCD_FIR *) NULL) hq_free(irs_ptr); /* Release memory */ free(down2_buff); free(down1_buff); free(up2_buff); free(up1_buff); free(irs_buff); free(fl_buff); free(sh_buff); /* Close files */ fclose(outfilptr); fclose(inpfilptr);#ifndef VMS return (0);#endif}/* ......................... End of main() ......................... */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -