Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about fft
#1
Hello,

New poster here. I apologize if this is a dumb question, but I'm trying to find some confirmation (or falsification) regarding my loose understanding of what is happening with the scipy fft funciton. I've pasted my code in here because I can't figure out how to attach my .py file.

import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft, fftshift

f = 10.0
overSampRate = 30.0
fs = overSampRate * f
phase = 1/3.0*np.pi
nCyl = 5.0
NFFT = 1024


def sine_wave(f,overSampRate,phase,nCyl):
	"""
	Generate sine wave signal with the following parameters
	Parameters:
		f : frequency of sine wave in Hertz
		overSampRate : oversampling rate (integer)
		phase : desired phase shift in radians
		nCyl : number of cycles of sine wave to generate
	Returns:
		(t,g) : time base (t) and the signal g(t) as tuple
	Example:
		f=10; overSampRate=30;
		phase = 1/3*np.pi;nCyl = 5;
		(t,g) = sine_wave(f,overSampRate,phase,nCyl)
	"""
	fs = overSampRate*f # sampling frequency
	t = np.arange(0,nCyl*1/f-1/fs,1/fs) # time base
	g = np.sin(2*np.pi*f*t+phase) # replace with cos if a cosine wave is desired
	return (t,g) # return time base and signal g(t) as tuple




#
# Plot time domain signal
#
(t,x) = sine_wave(f,overSampRate,phase,nCyl) #function call
fig1, ax1 = plt.subplots(nrows=1, ncols=1)
#plt.plot(t,x) # plot using pyplot library from matplotlib package
ax1.plot(t, x)
ax1.set_title('Sine wave f='+str(f)+' Hz') # plot title
ax1.set_xlabel('Time (s)') # x-axis label
ax1.set_ylabel('Amplitude') # y-axis label

#
# Calc freq domain
#
X = fft(x)   # not shifted around 0Hz/rad
X = X / (len(X)*1.0)
X = X * 2.0
nVals = np.arange(start=0, stop=len(X))#*fs/(NFFT*1.0)    # need to cast divisor (NFFT) to float
#nVals = np.arange(start=0, stop=NFFT)#*fs/(NFFT*1.0)    # need to cast divisor (NFFT) to float


# plot fft
fig2, ax2 = plt.subplots(nrows=1, ncols=1)
ax2.plot(nVals, np.abs(X))      # take abs() to convert to complex to real num
ax2.set_title('Double Sided FFT - with FFTShift')
ax2.set_xlabel('Sample points (N-point DFT)')
ax2.set_ylabel('DFT Values')

plt.show() 		# display the figure
In this code I basically make a sine wave and plug it into the fft function on line #50.

My questions are the following:
1. Is it correct to divide the result by N (number of samples in waveform) and then multiply by 2?
2. If #1 is correct, does the multiply by 2 come in because of the result of the fft always being half the expected magnitude due to the mathemeatics of the DFT?
3. If I do the following for the FFT calculation my output magnitude gets attenuated greatly.
-Change FFT calc on line 50 to this
X = fft(x, NFFT)   # not shifted around 0Hz/rad

Now that the NFFT parameter is passed the resulting magnitude is much smaller? What is going on here and why would someone pass this parameter to the FFT function?

Any help is greatly appreciated!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020