I would like to calculate the fourier coefficients of $\cos(2 \pi \frac{k}{T}+\phi)$ where $T \in \mathbb{N}$ is the period and is arbitrary but fixed, $k \in [1, N-1]$ is the number of oscillations per period and $\phi \in [0, 2\pi[$ the phase.
I have rewritten my function as a complex exponential function:
\begin{align} \begin{split} x(k, \phi, t) &= \cos\left(2 \pi \frac{k}{T} t + \phi\right) \\ &= \frac{\mathrm{e}^{i\left(2 \pi \frac{k}{T} t + \phi\right)} + \mathrm{e}^{-i\left(2 \pi \frac{k}{T} t + \phi\right)}}{2} \\ &= \left(\frac{1}{2} \mathrm{e}^{i \phi}\right) \mathrm{e}^{i2 \pi \frac{k}{T} t} + \left(\frac{1}{2} \mathrm{e}^{-i \phi}\right) \mathrm{e}^{-i2 \pi \frac{k}{T} t} \end{split} \end{align}
I then calculated my coefficients $c_n$ as:
\begin{align} \begin{split} c_n &= \frac{1}{T} \int\limits_{-\frac{T}{2}}^{\frac{T}{2}} x(k, \phi, t) \mathrm{e}^{-i n \frac{2\pi}{T}t} dt \\ &= \frac{1}{2T} \left( \mathrm{e}^{i \phi} \int\limits_{-\frac{T}{2}}^{\frac{T}{2}} \mathrm{e}^{i 2 \pi \frac{k - n}{T} t} dt + \mathrm{e}^{-i \phi} \int\limits_{-\frac{T}{2}}^{\frac{T}{2}} \mathrm{e}^{-i 2 \pi \frac{k + n}{T} t} dt \right) \end{split} \end{align}
I evaluated the integrals with $\operatorname{sinc}(x)=\frac{\sin(\pi x)}{\pi x}$: \begin{align*} I_1 &= \int \mathrm{e}^{i 2 \pi \frac{k - n}{T} t} dt\\ &= \frac{-iT}{2 \pi (k - n)} \mathrm{e}^{i 2 \pi \frac{k - n}{T} t} \end{align*}
\begin{align} \begin{split} I_1(t) \Bigr|_{-\frac{T}{2}}^{\frac{T}{2}} &= \frac{-iT}{2 \pi (k - n)} \left( \mathrm{e}^{i \pi \left( k - n \right)} - \mathrm{e}^{-i \pi \left( k - n \right)} \right) \\ &= T \frac{\sin\left(\pi \left(k - n\right)\right)}{\pi (k - n)} = T \operatorname{sinc}\left(k-n\right) \end{split} \end{align}
And analog the other integral as $T \operatorname{sinc}\left(k+n\right)$.
I then get my coefficients $c_n$: \begin{align} c_n = \frac{1}{2} \left( \mathrm{e}^{i \phi} \operatorname{sinc}\left(k-n\right) + \mathrm{e}^{-i \phi} \operatorname{sinc}\left(k+n\right) \right) \end{align}
I tested my $c_n$ using some examples in Mathematica with the FourierCoefficient function and they seemed to be correct.
I further wanted to check my $c_n$ by calculating an inverse fft and plotting them together with the original $x(k, \phi, t)$. But the results were not as expected.
Plot (blue is what was expected and red the result from IFFT) with $\phi=0.0$ and $k = 8.5$:

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?
Thank you for your help!
Use the sum formula $\cos(\alpha+\beta)=\cos\alpha\cos\beta-\sin\alpha\sin\beta$ and compute the Fourier coefficients on just the part with the variables.