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

前端 未结 5 2041
孤城傲影
孤城傲影 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条回答
  •  -上瘾入骨i
    2021-02-08 11:25

    With look up table approach one may wish to use the memory efficiently and store only the first quadrant of the sine wave.

    Then second quadrant = sin[180 - angle]  ; // for angles 90..180 degrees
    third quadrant = -sin[angle-180];          // for 180..270
    fourth quadrant = -sin[-angle+360]         // for 270..360
    

    I would recommend linear interpolation, but there's also vector rotation based approach for sine generation (that produces both sine and cosine simultaneously)

    x_0 = 1, y_0 = 0;
    x_n+1 = x_n * cos(alpha) - y_n * sin(alpha)
    y_n+1 = x_n * sin(alpha) + y_n * cos(alpha), 
    

    where alpha=phase difference of the frequency == 2pi*fHz/Ts, with fHz is frequency to be produced (in Hertz) and Ts is sampling time (or 1/Ts = sampling frequenzy eg. 44100 Hz).

    which leads to a resonating feedback filter approach, whose transfer function f(z) has two conjugate poles at unit circle (z=e^jomegaT).

    y[n] = y[n-1] - 2*cos(alpha) * y[n-2], with
    y[0] = 0,
    y[1] = sin(alpha)
    

    The fun part is that one can change the alpha (cos(alpha)) on the fly. The downside of this IIR filter approach is that it's unstable by definition. Floating point and especially fixed point inaccuracies accumulate and lead to either exponential decay, or exponential growth of the magnitude. That can however be cured with allowing a slight phase distortion.

    Instead of as in CORDIC rotation having a known per iteration amplification factor:

    K = sqrt(1+sin(alpha)^2);
    
    x' = x - y*sin(alpha);
    y' = y + x*sin(alpha);
    
    one can do
    
    x' = x - y * sin(alpha);
    y'' = y + x' * sin(alpha);
    

    which doesn't produce perfect circles for (x', y'') but stable ellipses even with fixed point arithmetic. (Note that this assumes relatively small values of alpha, meaning also relatively low frequencies.)

提交回复
热议问题