fft-chart.html
001|
002| from sys import argv
003| from os import path
004| from goodies import divisions
005| import numpy as np
006| import matplotlib.pyplot as plt
007| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
008|
009| BasePath = r'C:\CJS\prj\Python\ws\math'
010| FileName = 'fft.png'
011| i2pi = complex(0,1)*complex(2.0*np.pi,0)
012|
013| F1 = complex( 9.0, 0)
014| F2 = complex(21.0, 0)
015|
016| NbrCycles = 3
017| SampleRate = 600
018| MaxFreq = 50
019|
020| def make_chart (xs, ys1, ys2, ys3, xs4, ys4):
021| fig, axis = plt.subplots(4, 1)
022| fig.suptitle('Homemade Fourier Transform', fontsize=14, fontweight='bold')
023|
024| axis[0].set_xlim(0.0, 1.0)
025| axis[0].xaxis.set_major_locator(MultipleLocator(1.0))
026| axis[0].xaxis.set_minor_locator(MultipleLocator(0.1))
027| axis[0].set_ylim(-1.0, +1.0)
028| axis[0].yaxis.set_major_locator(MultipleLocator(1.0))
029| axis[0].yaxis.set_minor_locator(MultipleLocator(0.25))
030| axis[0].plot(xs, [c.real for c in ys1], c='#0000ff', lw=1)
031| axis[0].plot(xs, [c.imag for c in ys1], c='#ff0000', lw=1)
032|
033| axis[1].set_xlim(0.0, 1.0)
034| axis[1].xaxis.set_major_locator(MultipleLocator(1.0))
035| axis[1].xaxis.set_minor_locator(MultipleLocator(0.1))
036| axis[1].set_ylim(-1.0, +1.0)
037| axis[1].yaxis.set_major_locator(MultipleLocator(1.0))
038| axis[1].yaxis.set_minor_locator(MultipleLocator(0.25))
039| axis[1].plot(xs, [c.real for c in ys2], c='#0000ff', lw=1)
040| axis[1].plot(xs, [c.imag for c in ys2], c='#ff0000', lw=1)
041|
042| axis[2].set_xlim(0.0, 1.0)
043| axis[2].xaxis.set_major_locator(MultipleLocator(1.0))
044| axis[2].xaxis.set_minor_locator(MultipleLocator(0.1))
045| axis[2].set_ylim(-1.0, +1.0)
046| axis[2].yaxis.set_major_locator(MultipleLocator(1.0))
047| axis[2].yaxis.set_minor_locator(MultipleLocator(0.25))
048| axis[2].plot(xs, [c.real for c in ys3], c='#0000ff', lw=1)
049| axis[2].plot(xs, [c.imag for c in ys3], c='#ff0000', lw=1)
050| axis[2].plot(xs, [abs(c) for c in ys3], c='#000000', lw=1)
051|
052| axis[3].set_xlim(xs4[0], xs4[-1])
053| axis[3].xaxis.set_major_locator(MultipleLocator(5.0))
054| axis[3].xaxis.set_minor_locator(MultipleLocator(1.0))
055| axis[3].set_ylim(0.0, +1.0)
056| axis[3].yaxis.set_major_locator(MultipleLocator(1.0))
057| axis[3].yaxis.set_minor_locator(MultipleLocator(0.1))
058| axis[3].bar(xs4, ys4, color='#0000ff', width=0.3)
059|
060| fig.set_figwidth(6.0)
061| fig.set_figheight(6.0)
062| fig.subplots_adjust(left=0.06, right=0.98, bottom=0.08, top=0.95, hspace=0.30)
063| fig.savefig(path.join(BasePath,FileName))
064|
065| Ticks = list(divisions(0.0, NbrCycles, SampleRate*NbrCycles))
066|
067| def fourier (wf):
068| bins = []
069| for f in divisions(0.0, MaxFreq, MaxFreq*10):
070| tot = complex()
071| for x,amp in zip(Ticks,wf):
072| p = np.exp(i2pi*f*x) * amp.real
073| tot += p
074| bins.append((f,tot))
075| return bins
076|
077|
078|
079| def main (*args):
080|
081| wf1 = [np.exp(i2pi*F1*complex(x,0)) for x in Ticks]
082| wf2 = [np.exp(i2pi*F2*complex(x,0)) for x in Ticks]
083| wf0 = [(a+b)/2.0 for a,b in zip(wf1,wf2)]
084|
085|
086| fts = fourier(wf0)
087| fts_r = [c.real for x,c in fts]
088| fts_i = [c.imag for x,c in fts]
089|
090|
091| m0r = float(min(fts_r))
092| m1r = float(max(fts_r))
093| m0i = float(min(fts_i))
094| m1i = float(max(fts_i))
095|
096|
097| m = m1r * 1.01
098| ys4 = [x/m for x in fts_r]
099| xs4 = [x for x,c in fts]
100|
101| make_chart(Ticks, wf1,wf2,wf0, xs4,ys4)
102|
103| return (xs4, ys4)
104|
105|
106| if __name__ == "__main__":
107| print('autorun: %s' % argv[0])
108| rv = main(*argv[1:])
109| print(*rv, sep='\n')
110|
111| '''eof'''
112|