Search code examples
csignalssignal-processingfrequency

How to reduce sample rate using integer division?


I want to reduce my base frequency F (80000 Hz) by an arbitrary fraction using only integer devision.

Let's say that new_sample() function is called with a frequency F. Then I can reduce sample rate to 40000 Hz using code below.

i = 0; // global counter
new_sample(value){
    if(i % 2 == 0){
        add_sample_to_buffer(value);
    }else{
        // skip sample
    }
    i++;
}

What if I want to reduce sample rate by a factor of 1.6 to 50000 Hz?

As Clifford pointed out: I want to change sample rate without changing the frequency. Basically I want to undersample the signal by skipping some samples in real time.

p.s.: I know that prescaler and postcaler are usualy used to reduce frequiency. I dont know if they allow to reduce frequency by a fraction.


Solution

  • Try this:

    unsigned Nanoseconds = 0; // global counter
    
    void new_sample(value){
        Nanoseconds += 12500; // interval between calls at 80000 
        if(Nanoseconds>20000){
            Nanoseconds-=20000;
            add_sample_to_buffer(value);
        }else{
            // skip sample
        }
    }
    

    During one second Nanoseconds will be incremented 80000 times by 12500 which is equal 1E9, and will be 50000 times decremented by 20000.