I got an $N$, in my case 512, point FFT of a real-valued signal. Based on some calculation in my application I determine the parameters $k \in [1, N-1]$, the number of oscillations per period, $\phi \in [0, 2\pi[$, phase. I use these parameters and the function $\cos\left(2\pi\cdot \frac{k}{N}\cdot t + \phi\right)$ to describe a signal in time-domain. Instead of calculating all the points of the signal and then doing an FFT I would like to directly calculate the result in frequency domain.
I calculated the FFT coefficients for the function as following:
$$c_n = \frac{N}{2}\left(\mathrm{e}^{i\phi} \cdot \mathrm{sinc}\left(n-k\right)+\mathrm{e}^{-i\phi} \cdot \mathrm{sinc}\left(n+k\right)\right), \quad\text{ with }\mathrm{sinc}(x) = \frac{\sin(\pi x)}{\pi x}$$
Note: $N$ is in here because the output of the FFT algorithm I use is not scaled and scaling is done in IFFT:
I verified my coefficients using Mathematica with the FourierCoefficient function.
I calculated a 512 elements long vector of complex doubles in Matlab and set my first vector to zero as there is no (dc) offset.
- Is this valid or do I have to calculate bin 0 too if my $k$ is not a whole number?
- Can I use $c_0$ for it?
The next 255 values I filled $c_n$ with $n$ from -1 to -255. The next value I filled with zero as I was unsure which coefficient should go there. And then the next 255 values I filled with $c_n$ with $n$ from 255 to 1.
To verify the correctness of my generated signal I plotted the real value of the IFFT of my vector. The output was as expected as long as $\phi$ was 0 and $k$ was a whole number.
Plot (blue is what was expected and red the result from IFFT) with $\phi=0$ and $k = 8.3$:
Plot with $\phi=0.3$ and $k = 8$:
Plot with $\phi=0.3$ and $k = 8.3$:
For reference my matlab code is:
k = 8.3; %number of oscillations per period
phi = 0.3; %phase in radians
t = linspace(0,511,512);
n = linspace(-1,-255,255);
fft_coeff0 = 256*(exp(i*phi)*sinc(-k)+exp(-i*phi)*sinc(k));
fft_coeffs(1:255) = 256*(exp(i*phi)*sinc(n-k)+exp(-i*phi)*sinc(n+k));
reconst_fft = [fft_coeff0 fft_coeffs 0 conj(fft_coeffs(end:-1:1))];
y = cos(k/512*2*pi*t+phi);
plot(t, ifft(reconst_fft), 'r', t, y, 'b')
- What could cause the difference between my expected output and the IFFT?
- How can I fix it?
EDIT: Changed code to only calculate one side and use complex conjugating for the other side and added $c_0$ coefficient to vector.
Plot with $\phi=0.0$ and $k = 8.5$:
Adding $c_0$ fixed the wrong amplitude.