Search code examples
chistogramfrequency

kind of non-visual frequency histogram in C


I would like to make a kind of frequency histogram. I have a binary file describing a song with double between -1 and 1 ( like -0.001235 ) and i would like to make a fonction that gives me the number of values between 0.9 and 1 for example. But where it's not easy for, it that the space of interval can change.

It's like, I want 40 or 50 or 100 interval between -1 and 1, and couting the number of double in those interval. Is it all clear ? ^^'

e.g : [-1,-0.9] -> 152 values .... [-0.9,-0.8] -> 34 values ....

I have already an array of 1024 frequency double (*tab)[1024]; thank you for your help :)


Solution

  • First you need a function which counts all frequences in an interval in on table:

    int countinterval( double *tab, int tabsize, double min, double max )
    {
        int count = 0;
        for ( int i = 0; i < tabsize; i ++ )
        {
            if ( tab[i] >= min && tab[i] < max ) // test if frequency is in interval [min,max[
            // if ( tab[i] >= min && tab[i] <= max ) // test if frequency is in interval [min,max]
                count ++;
        }
        return count;
    }
    

    Next you need a function which sums the results of each table.

    int counttabsinterval( double *tab[], int tabsize, int tabcount, double min, double max )
    {
        int count = 0;
        for ( int i = 0; i < tabcount; i ++ )
            count += countinterval( tab[i], tabsize, min, max ); // sums results
        return count;
    }
    

    Call the function for each interval where you want to know number of frequencies.

    double *tab[1024];
    int tabsize = ???; // <- init with size of one table
    int count1 = counttabsinterval( tab, tabsize, 1024, -1.0, -0.9 );
    int count2 = counttabsinterval( tab, tabsize, 1024, -0.9, -0.8 );
    

    Here is a function which calculates the Quantitative Frequency Distribution for on table tab with lenght of table tabsize ans stores the result in an array distwith length distcount.

    void QuantitativeFrequencyDistribution( double *tab, int tabsize, double *dist, int distcount )
    {
        int count = 0;
        double intervallAmount = 2.0 / distcount;
        for ( int i = 0; i < distcount; i ++ )
        {                            
            double min = -1.0 + i*intervallAmount;
            double max = min + intervallAmount;
            dist[i] = countinterval( tab, tabsize, min, max );
        }
    }
    
    int sizeOfTab = ?;// <- init with size of table
    double *tab; // <- your table
    int distcount = 20; // for example
    double dist[distcount];
    QuantitativeFrequencyDistribution( tab, sizeOfTab, dist, distcount );
    

    If you have different tables with different lengths and you want to know the Quantitative Frequency Distribution over all tables, you have to call QuantitativeFrequencyDistribution for each single table and calculate sum of the results.