-2

I'm using Python. Input is a .csv file with timestamp, current (AC). I'm able to plot the FFT of a signal and I see the harmonics. I'd like to find and output(or display on the plot the x and y coordinates) the magnitude of the harmonics (e.g. every magnitude out to the 50th harmonic). What would be the best method for doing so? Thank you.


Update

Here's the plot that I have: Harmonic plots

My signal is 60Hz, so I know my harmonics will be at 120, 180,...

What I'm trying to do, is to find or output the magnitude of all the harmonics (from 2nd up to the 50th). So I'd like to ask there's a good way to approach this.

Ultimately, what I'm trying to achieve is to be able to calculate the harmonic amplitude to the fundamental (percentage).


Update

I ended up using zip to map the frequency and amplitude, then printed it out. Here's my code:

import pandas as pd
import numpy as np
import plotly
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft
from scipy import arange
import csv
import math

df=pd.read_csv (r'C:\Users\20amps.csv')
N=df.shape[0] #Number of samples
T=125000 #Frequency of Signal
k=arange(N)
Ts=N/T
freq=k/Ts
freq=freq[range(N//40)]

amp=df["AC1 A"]
rms= [i * math.sqrt(2) for i in amp]
yf=fft(rms)/N
yf=yf[range(N//40)]

zipped = zip (freq, abs(yf))
print (zipped)
w = list(zipped)
print(w)

#Writing to csv file
#with open('data.csv', 'w') as csvFile: 
#    writer =csv.writer(csvFile, delimiter=',')
#   writer.writerow(w)

plt.plot(freq, abs(yf),'r') #plot results
plt.grid()
plt.xlabel('Frequency')
plt.ylabel(r'Amplitude')
plt.show()  

Then I would just look up the frequencies and amplitude that I need.

Would be open to suggestion on better ways to do this.

gliu
  • 3
  • 1
  • 4
  • Clarify your question a little please. Are you trying to see the higher harmonics from the first peak (e.g. 50Hz)? Why not just collect enough data points, do an FFT that's wide enough and the plot will naturally show you the higher harmonics (100, 150, 250, etc). – Martin Dinov Oct 03 '19 at 23:03
  • Thanks. I updated my post to clarify. – gliu Oct 03 '19 at 23:51

1 Answers1

0

Just look at the amplitude in the corresponding frequency bins. You can write a method to define the fundamental frequency of interest (e.g. 60Hz), then you know which frequency bins to look at for the harmonics (120Hz, 180Hz, etc). If you setup your DFT right, you know the frequency range within and between the bins.

This stackoverflow post goes into more detail on how to compute the bins properly and what the different parameters mean for you: how to extract frequency associated with fft values in python

Again, if you know your bin widths, it's trivial to check the power/amplitude at a given harmonic, since you know which bin(s) to look at.

Hope that gets you on the right track.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41