# How to sample continuous signal correctly?

import numpy as np
import matplotlib.pyplot as plt

j = 1j
pi = np.pi

interval = 0.5
fs = 1024
Ts = 1/fs
N = np.uint(interval*fs)
t = np.linspace(0, interval - Ts, N)
freq = 2*pi/N * np.arange(N)
f0 = 16

# x(t) = sin(2pi * f0 * t)
# a[n] = x(n*Ts) = sin(2pi * f0 * n * Ts)
n = np.arange(N)
a = np.sin(2*pi*f0*Ts*n)

# visualisation de a
# on ajoute a droite la valeur de gauche pour la periodicite
plt.subplot(311)
plt.plot( t, a)

# calcul de A
A = 1/N * np.fft.fft(a)

# visualisation de A
# on ajoute a droite la valeur de gauche pour la periodicite

plt.subplot(312)
plt.plot(freq, np.real(A))

plt.ylabel("Real Part")

plt.subplot(313)
plt.plot(freq, np.imag(A))

plt.ylabel("Imaginary Part")

plt.show()


So I have a signal $$x(t) = \sin(2\pi \cdot f_0 \cdot t),\ t = 0\rightarrow0.5s$$

I wish to sample the signal at $$f_s = 1024\ \mathrm{H_z} \longrightarrow T_s = 1/1024$$

So I make $$a[n] = x(n\cdot T_s) \longrightarrow a[n] = \sin(2\pi\cdot f_0\cdot T_s\cdot n)$$

But clearly, this makes the discrete signal have $$f = f_0\cdot T_s$$.

So when I FFT $$a[n]$$, the 2 frequency spike is at the wrong location. You can see that the $$\sin$$ spike is at $$2\pi\cdot f_0\cdot T_s$$, instead of $$2\pi\cdot f_0$$

I don't want to make $$a[n] = \sin(2\pi\cdot f_0\cdot n)$$ because then I only get the values of $$x(t)$$ at positions $$t = 2\pi\cdot f_0\cdot n$$

Which step did I forget (or did wrong)? How to sample the $$x(t)$$ so that the FFT is correct? Thank you.

• The relative frequency in discrete time can be defined as $f=F/F_s$ which you got it right with your equation $f=f_0 * T_s$. If you perform FFT on $a[n]$ you may see the spectral content concentrated at $f$. – jomegaA Feb 24 '20 at 11:13
• The FFT of $\sin$ whose spectrum might resemble like a spike must be at $f=f_0*T_s$, if you plotted using relative frequency instead of $n$ – jomegaA Feb 24 '20 at 11:16
• so what should I add so that the graph has 2 spike at 2*pi*16 ? (the original frequency in continuous time) – Duke Le Feb 24 '20 at 11:44
• You may use fftshift (python equivalent I have no idea)... – jomegaA Feb 24 '20 at 13:02

close all;
clear all;

interval        = 0.5;
f0              = 16;
t               = 0:1/2048:interval-(1/2048)
x_t             = sin(2*pi*f0*t);

figure(1);
subplot(2, 1, 1);
plot(t, x_t);
axis([0 interval])
hold on;

fs              = 1024;
N               = (interval * fs);
sample_index    = [0:N-1];
x_n             = sin(2*pi*(f0/fs)*sample_index);

n               = 0:1/fs:interval-(1/fs)

stem(n, x_n);

X               = fftshift(fft(x_n, N));

df              = fs / N;
sampleIndex     = -N/2:N/2-1;
relative_f      = sampleIndex * df;

subplot(2, 1, 2);
stem(relative_f, abs(X));
xlabel('f (Hz)'); ylabel('|X(k)|');
axis([min(relative_f) max(relative_f)])