How to look up sine of different frequencies from a fixed sized lookup table?

前端 未结 5 2048
孤城傲影
孤城傲影 2021-02-08 11:06

I am sampling a sine wave at 48 kHz, the frequency range of my sine wave can vary from 0 to 20000 Hz with a step of about 100 Hz. I am using a lookup table approach. So I genera

5条回答
  •  悲&欢浪女
    2021-02-08 11:17

    If you want high precision, you can use a trig identities to have both a small LUT, and clean sine waves.

    static float gTrigSinHi[256], gTrigSinLo[256];
    static float gTrigCosHi[256], gTrigCosLo[256];
    
    ////////////////////////////////////////
    // Sets up the fast trig functions
    void FastTrigInit()
    {
        unsigned i;
        for(i = 0; i < 256; ++i)
        {
            gTrigSinHi[i] = sin(2.0 * M_PI / 0xFFFF * (i << 8));
            gTrigSinLo[i] = sin(2.0 * M_PI / 0xFFFF * (i << 0));
            gTrigCosHi[i] = cos(2.0 * M_PI / 0xFFFF * (i << 8));
            gTrigCosLo[i] = cos(2.0 * M_PI / 0xFFFF * (i << 0));
        }
    }
    
    ////////////////////////////////////////
    // Implements sin as
    //      sin(u+v) = sin(u)*cos(v) + cos(u)*sin(v)
    float FastSin(unsigned short val)
    {
        unsigned char hi = (val >> 8);
        unsigned char lo = (val & 0xFF);
        return gTrigSinHi[hi] * gTrigCosLo[lo]
            + gTrigCosHi[hi] * gTrigSinLo[lo];
    }
    
    ////////////////////////////////////////
    // Implements cos as
    //      cos(u+v) = cos(u)*cos(v) - sin(u)*sin(v)
    float FastCos(unsigned short val)
    {
        unsigned char hi = (val >> 8);
        unsigned char lo = (val & 0xFF);
        return gTrigCosHi[hi] * gTrigCosLo[lo]
            - gTrigSinHi[hi] * gTrigSinLo[lo];
    }
    

提交回复
热议问题