// file: fbc.C // // Main program file. // EE4012: Design of an Automatic Feedback Detection and Elimination System #include "fbc.h" main(int argc, char** argv) { // process the command line // char output_file[MAX_STRING_LENGTH]; char input_file[MAX_STRING_LENGTH]; // define parameters // float sample_frequency = 8000.0; float frame_duration = 0.100; float window_duration = 0.150; // initialize filter delays // float yn1 = 0.0; float yn2 = 0.0; float xn1 = 0.0; float xn2 = 0.0; // declare filter coefiencents // float a1 = 0.0; float a2 = 0.0; float b1 = 0.0; float b2 = 0.0; float a1_prev = a1; float a2_prev = a2; float b1_prev = b1; float b2_prev = b2; // declare variable for peak in fft_out and its sample place holder // declare the threshold for advanced detection process // float max = 0.0; float threshold = 0.0; float center_freq = 0.0; // Number of FFT points // int fft_num = 32; // initialize center_freq and bandwidth for testing purposes // float bandwidth = 10; // process command line // if (fbc_cmdl_0_C(output_file, input_file, sample_frequency, fft_num, frame_duration, window_duration, argc, argv) == L_FALSE) { printf("%s: error parsing command line\n", PROGRAM_NAME); return(ISIP_ERROR); } // Compute bandwidth to equal the resolution of the fft // bandwidth = (sample_frequency / fft_num) * 1.5; // declare variable for peak in fft_out and its sample place holder // declare the threshold for advanced detection process // float* max2 = (float*)malloc((fft_num / 2) * sizeof(float)); float threshold_const2 = 5.0; float* center_freq2 = (float*)malloc((fft_num / 2) * sizeof(float)); int num_maximums2 = 0; // aravinds stuff: set up the FFT to be used as a function call // Fourier_transform fft; fft.set_cc(fft_num,4,0); // process file or input // // The next section of code is the bulk of the algorithm. It opens the input file // creates an output file, computes the size of the file, allocates the buffers, // then loops over the processing section of the code. // The processing section of code reads in the data, computes one frame of output // data from one analysis window, performs a fft on the window, detects feedback in // that frame, applies the filter to the frame, then writes the processed data to // an output file. // After this the input and output files are closed, the buffers are deallocated, and // finally exits. // open the input file // FILE* fp_in = fopen(input_file, "r"); if (fp_in == NULL) { printf("%s: error opening input file\n", input_file); return L_FALSE; } // create the output file // FILE* fp_out = fopen(output_file, "w"); if (fp_out == NULL) { printf("%s: error creating output file\n", output_file); return L_FALSE; } // compute the size of the file (this makes the code a little easier) // fseek(fp_in, 0, 2); int size_in_samples = ftell(fp_in) / sizeof(short int); rewind(fp_in); float num_frames = fbc_round_C((size_in_samples / sample_frequency) / frame_duration); // Compute the number of samples in each window and each frame // int window_dur_samples = (int)fbc_round_C(window_duration * sample_frequency); int frame_dur_samples = (int)fbc_round_C(frame_duration * sample_frequency); // Determine if the number of samples in a window is a value that is // 4^x. This is a requirement for the input to the Radix-4 FFT. The // input array for the fft must be of this size. The array will // be zero padded to all proper processing to the fft. // int powerofx = 0; while(pow(4,powerofx) < window_dur_samples){ powerofx = powerofx + 1; } int powerof_4_size = pow(4,powerofx); // Allocate buffers // // buffer_in: buffer of one window of data // buffer_out: buffer of one frame of data // temp_buffer_out: the zero padded buffer of buffer_in // ( a zero padded window ) // fft_out: the real and imaginary output of the fft of the window, // 0 to fs points // filtered_frame: frame of data that has been filtered float* buffer_in = (float*)malloc(window_dur_samples * sizeof(float)); float* buffer_out = (float*)malloc(frame_dur_samples * sizeof(float)); float* filtered_frame = (float*)malloc(frame_dur_samples * sizeof(float)); double* fft_out = (double*)malloc(2 * powerof_4_size * sizeof(double)); double* temp_buffer_in = (double*)malloc(powerof_4_size * sizeof(double)); // loop over the entire file // for (int frame_number=0; frame_number