Frequency domain of a sine wave with frequency 1000Hz

☆樱花仙子☆ 提交于 2019-12-11 17:18:37

问题


I'm starting DSP on Python and I'm having some difficulties: I'm trying to define a sine wave with frequency 1000Hz

I try to do the FFT and find its frequency with the following piece of code:

import numpy as np

import matplotlib.pyplot as plt

sampling_rate = int(10e3)
n = int(10e3)

sine_wave = [100*np.sin(2 * np.pi * 1000 * x/sampling_rate) for x in range(0, n)]

s = np.array(sine_wave)

print(s)

plt.plot(s[:200])
plt.show()

s_fft = np.fft.fft(s)
frequencies = np.abs(s_fft)
plt.plot(frequencies)
plt.show()

So first plot makes sense to me. Second plot (FFT) shows two frequencies: i) 1000Hz, which is the one I set at the beggining ii) 9000Hz, unexpectedly freqeuncy domain


回答1:


Your data do not respect Shannon criterion. you do not set a correct frequencies axis.

It's easier also to use rfft rather than fft when the signal is real.

Your code can be adapted like :

import numpy as np
import matplotlib.pyplot as plt

sampling_rate = 10000
n = 10000
signal_freq = 4000 # must be < sampling_rate/2 
amplitude = 100

t=np.arange(0,n/sampling_rate,1/sampling_rate)
sine_wave = amplitude*np.sin(2 * np.pi *signal_freq*t) 
plt.subplot(211)
plt.plot(t[:30],sine_wave[:30],'ro')

spectrum = 2/n*np.abs(np.fft.rfft(sine_wave))
frequencies = np.fft.rfftfreq(n,1/sampling_rate)
plt.subplot(212)
plt.plot(frequencies,spectrum)
plt.show()

Output :

There is no information loss, even if a human eye can be troubled by the temporal representation.



来源:https://stackoverflow.com/questions/55317667/frequency-domain-of-a-sine-wave-with-frequency-1000hz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!