Jay Kim
Jay Kim

Reputation: 123

reconstruct signal from fft

Im reconstructing signal from amplitude, frequency and phase obtained fft. After I do fft, I picked some of its frequencies and reconstructed time line signal from those fft data. I know IFFT is for this but, I dont want to use IFFT. Reconstruction seems fine but theres some time lag between two signals. This image shows this problem. Black one is the original signal and red one is that reconstructed. enter image description here

If I know correctly, amplitude of frequency bin t is sqrt(real[t]*real[t] + imag[t]*imag[t] and phase is atan2(imag[t], real[t]). So, I used formula amplitude * cos(2*π*x / frequency + phase) for a frequency bin. And I summed those regenerated waves. As far as I know, this should generate intact signal fits to original signal. But it ends up always with some time lag from original signal.

Yeah, I think its about phase but thats so simple to calculate and its working correctly. If it has error, reconstructed signal would not fit to its original signal in shape.

This is the code to generate cosine wave. I generated cosine wave from sin(x + π/2).

std::vector<short> encodeSineWavePCM(
    const double frequency,
    const double amplitude,
    const double offSetPhase)
{
    const double pi = 3.1415926535897932384626;
    const int N = 44100; // 1 sec length wave
    std::vector<short> s(N);
    const double wavelength = 1.0 * N / frequency;
    const double unitlength = 2 * pi / wavelength;
    for (int i = 0; i<N; i ++) {
        double val = sin(offSetPhase + i * unitlength);
        val *= amplitude;
        s[i] = (short)val;
    }

    return s;
}

What am I missing?

Upvotes: 2

Views: 2770

Answers (1)

MSalters
MSalters

Reputation: 179897

Quite normal. You're doing a frame-by-frame transform. That means the FFT frame is produced after one time frame. When transforming back, you have the inverse effect: your time frame starts after the FFT frame has been decoded.

Upvotes: 1

Related Questions