Exemplo n.º 1
0
//! apply ChebyshevI filter on a matrix
//! @param[in] matrix	matrix to be filtered
//! @return 			filtered matrix
mat ChebyshevFilter(mat matrix)
{
	float **floatMatrix = matToFloat(matrix);
	Dsp::SimpleFilter<Dsp::ChebyshevI::LowPass<5>,3> filter;
	int filterOrder = 2;
	int samplingFreq = 44;
	float cutFreq = 0.25;
	float passRipple = 0.001;

//	cout<<"ChebyshevFilter: floatMatrix"<<endl;
//	for (int i = 0; i < matrix.n_rows; i++)
//	{
//		for (int j = 0; j < matrix.n_cols; j++)
//			cout<< floatMatrix[i][j]<<" ";
//		cout<<endl;
//	}

	filter.setup(filterOrder, samplingFreq, cutFreq, passRipple);
	filter.process(matrix.n_cols, floatMatrix);
	mat lowpassComponent = floatToMat(floatMatrix, matrix.n_rows, matrix.n_cols);
//	cout<<"ChebyshevFilter: gravity "<<endl;
//	lowpassComponent.print();

	return lowpassComponent;
}
Exemplo n.º 2
0
//! apply ChebyshevI filter on a matrix
//! @param[in] matrix	matrix to be filtered
//! @return 			filtered matrix
mat ChebyshevFilter(mat matrix)
{
	float **floatMatrix = matToFloat(matrix);
	Dsp::SimpleFilter<Dsp::ChebyshevI::LowPass<5>,3> filter;
	int filterOrder = 2;
	int samplingFreq = 32;
	float cutFreq = 0.25;
	float passRipple = 0.001;

	filter.setup(filterOrder, samplingFreq, cutFreq, passRipple);
	filter.process(matrix.n_cols, floatMatrix);
	mat lowpassComponent = floatToMat(floatMatrix, matrix.n_rows, matrix.n_cols);

	return lowpassComponent;
}
Exemplo n.º 3
0
    int main(int argc, char **argv) {


        if (argc != 4) {
            exit_with_help();
        }

        LOGD( "start to get feature of audio file" );

        FILE *soundfile = NULL;
        FILE *chirpfile = NULL;
        FILE *featurefile = NULL;
        char *sound_filename = NULL;
        char *chirp_filename = NULL;
        char *feature_filename = NULL;
        sound_filename = argv[1];
        chirp_filename = argv[2];
        feature_filename = argv[3];

        int numSample = 0;
        int rawaudioSignals[200000];
        int value;


        int i2 = 0;
        float chirp[301];
        float chirp_value;

        if (sound_filename) {
            soundfile = fopen(sound_filename, "r");
            if (soundfile == NULL) {
                fprintf(stderr, "can't open file %s\n", sound_filename);
                exit(1);
            }
        }

        while (fscanf(soundfile, "%d", &value) > 0) {
            rawaudioSignals[numSample] = value;
            numSample++;
            if (numSample == 199998) {
                break;
            }
        }

        if (chirp_filename) {
            chirpfile = fopen(chirp_filename, "r");
            if (chirpfile == NULL) {
                fprintf(stderr, "can't open file %s\n", chirp_filename);
                exit(1);
            }
        }

        while (fscanf(chirpfile, "%f", &chirp_value) > 0) {
            chirp[i2] = chirp_value;
            i2++;
        }


        double *audio_signal = (double *) malloc(numSample * sizeof(double));
        if (audio_signal == NULL) {
            /* we have a problem */
            fprintf(stderr, "Error: out of memory.\n");
            return 1;
        }


        for (int j = 0; j < numSample; j++) {
            audio_signal[j] = (double) rawaudioSignals[j];
        }

        double *match_signal = (double *) malloc(numSample * sizeof(double));
        if (match_signal == NULL) {
            /* we have a problem */
            fprintf(stderr, "Error: out of memory.\n");
            return 1;
        }


        Dsp::SimpleFilter<Dsp::Butterworth::BandPass<5>, 1> f;
        f.setup(5,    // order
                48000,// sample rate
                15000, // center frequency
                3000);  // band width);
        f.process(numSample, &audio_signal);

//    FILE *outf_filter = fopen("filter1_0403_test2.txt", "w");
//
//    for ( int j=0; j<numSample; j++ )
//    {
//        //printf("%f\n", audio_signal[j]);
//        fprintf(outf_filter, "%f  ", audio_signal[j]);
//    }



        convCalculate(chirp, audio_signal, match_signal, numSample);

        FILE *outf_conv = fopen("convert1_0403_22_sound11.txt", "w");

//        for (int j = 0; j < numSample; j++) {
//            //printf("%f\n", audio_signal[j]);
//            fprintf(outf_conv, "%f  ", match_signal[j]);
//        }


//        printf("%d\n", numSample);

//    FILE *outf = fopen("results1_0307_test2.txt", "w");
//
//    for ( int j=0; j<numSample; j++ )
//    {
//        //printf("%f\n", audio_signal[j]);
//        fprintf(outf, "%f  ", audio_signal[j]);
//    }

        int duration1 = 2300;
        int duration2 = 2000;
        int f_start = 600;
        int f_end = 1500;
        int focus_duration = 900;
        int step = 100;
        int peak_threshold = 200;
        int threshold = 200000;
        int threshold2 = 2000000;

        double tmp_whole_max = 0;
        int tmp_whole_max_index = 0;

        double tmp_echo_max = 0;
        int tmp_echo_max_index = 0;

        double tmp_max = 0;
        int tmp_max_index = 0;

        double tmp_exceed_threshold = 0;
        int tmp_exceed_index = 0;

        int focus_start_list[10];
        int focus_end_list[10];
        int focus_count = 0;

        int whole_start = 0;


        tmp_whole_max_index = findMaximum(&match_signal[0], whole_start, numSample, tmp_whole_max);
//    printf("%d\n", tmp_whole_max_index);
//    printf("%f\n", tmp_whole_max);

        while (tmp_whole_max > threshold) {

            tmp_exceed_index = ExceedThreshold(&match_signal[0], whole_start, numSample, threshold,
                                               tmp_exceed_threshold);
//            printf("process raw sound file  %s\n", sound_filename);
//            printf("tmp_exceed_index  %d\n", tmp_exceed_index);
//            printf("tmp_whole_max  %f\n", tmp_whole_max);

            int tmp_start = tmp_exceed_index;
            int tmp_end = tmp_exceed_index + duration1;
            whole_start = tmp_end;
//            printf("whole_start  %d\n", whole_start);
            if ((tmp_start < 0) | (tmp_end > numSample)) {

                break;
            }
            tmp_max_index = findMaximum(&match_signal[0], tmp_start, tmp_end, tmp_max);
//            printf("tmp_max_index  %d\n", tmp_max_index);
//            printf("tmp_max  %f\n", tmp_max);


            int tmp_end2 = tmp_max_index + duration2;

            tmp_echo_max_index = findMaximum(&match_signal[0], (tmp_max_index + f_start), (tmp_max_index + f_end),
                                             tmp_echo_max);

            if ((tmp_end2 > tmp_end) || (tmp_echo_max > threshold2)) {
                tmp_whole_max_index = findMaximum(&match_signal[0], whole_start, numSample, tmp_whole_max);
                continue;
            }
            focus_start_list[focus_count] = tmp_max_index + f_start;
            focus_end_list[focus_count] = tmp_max_index + f_end;
            focus_count = focus_count + 1;


            tmp_whole_max_index = findMaximum(&match_signal[0], whole_start, numSample, tmp_whole_max);

        }

//        for (int i = 0; i < focus_count; i++) {
//            printf("%d\t", focus_start_list[i]);
//            printf("%d\n", focus_end_list[i]);
//
//        }

        double *ave_focus = (double *) malloc(focus_duration * sizeof(double));
        if (ave_focus == NULL) {
            /* we have a problem */
            printf("Error: out of memory.\n");
            return 1;
        }

        average(&match_signal[0], &focus_start_list[0], focus_count, focus_duration, &ave_focus[0]);

        double Energy = getEnergy(&ave_focus[0], focus_duration);

        int numPeaks = 0;
        numPeaks = findPeaks(&ave_focus[0], focus_duration, peak_threshold);
//        printf("\n\n%d\n", numPeaks);

        double mean_var = 0.0;
        double max_var = 0.0;

        getVarResult(&ave_focus[0], focus_duration, step, mean_var, max_var);
//        printf("%f\n", mean_var);
//        printf("%f\n", max_var);


        if (feature_filename) {
            featurefile = fopen(feature_filename, "w");
            if (featurefile == NULL) {
                fprintf(stderr, "can't open file %s\n", feature_filename);
                exit(1);
            }
        }


        fprintf(featurefile, "0 ");
        fprintf(featurefile, "1:%f ", Energy);
        fprintf(featurefile, "2:%d ", numPeaks);
        fprintf(featurefile, "3:%f ", mean_var);
        fprintf(featurefile, "4:%f\n", max_var);


//        printf("finish raw sound file  %s\n", sound_filename);

        free(audio_signal);
        free(match_signal);
        free(ave_focus);
//    fclose(outf);
//    fclose(outf_conv);
        fclose(soundfile);
        fclose(chirpfile);
        fclose(featurefile);

        return 0;


    }