Recently I found my old code to transform sine wave from time domain to frequency domain. Below I post simplified version of it.

from math import sin, pi
from matplotlib.pyplot import *
from pylab import zeros, fft

fs = 500
duration = 2 * pi
amplitude = 2
frequency = 25
values_in_time_domain = []

for t in range(int(duration * fs)):
    values_in_time_domain.append(amplitude * sin(frequency * t / fs))

subplot(2, 1, 1)
title('Sinus from 0 to 2 pi in time domain; A = 2, f = 25, fs = 500')

transform = fft(values_in_time_domain)
transform_abs = [abs(x) for x in transform]
values_in_freq_domain = [transform_abs[f] / fs / pi for f in range(int(fs / 2)+1)]

subplot(2, 1, 2)
title('Above sinus in frequency domain')


Why do I have to divide every element from transform_abs by pi in the following: transform_abs[f] / fs / pi? I noticed I need to do it in order to have properly scaled $O_y$ axis.


The FFT should be divided by the number of elements in the vector. You specified the vector length to be duration = int(2*pi*fs) which is a really weird way to defining the length, btw. Thus, you need to divide by int(2*pi*fs). Yes, you need to divide by a 2 as well, because the signal energy is distributed evenly between positive and negative frequencies.

| improve this answer | |
  • $\begingroup$ I chose 2 * pi to have "full" sine wave. Thanks for clarification but I don't get one thing. If I would divide by two, the amplitude on $O_y$ would be wrong. The code I provided works well (at least seems to work well). $\endgroup$ – Luke Jan 9 '16 at 19:53
  • $\begingroup$ If your sine wave in time domain has amplitude of 1 and frequency fs, the transform will have a spike at fs with height of 1/2, and a spike at -fs with height of 1/2. $\endgroup$ – CMDoolittle Jan 9 '16 at 19:55
  • $\begingroup$ So spikes have a height of half of the amplitude. Thanks for the explanation. $\endgroup$ – Luke Jan 9 '16 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.