fft-2.html
001|
002| from sys import argv
003| from os import path
004| import numpy as np
005| import matplotlib.pyplot as plt
006| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
007|
008| BasePath = r'C:\CJS\prj\Python\ws\math'
009| ImgsPath = BasePath
010| Filename = 'fourier-2.png'
011|
012| a = float(argv[1]) if 1 < len(argv) else 1.00
013| b = float(argv[2]) if 2 < len(argv) else 8.00
014| c = float(argv[2]) if 2 < len(argv) else 0.1
015|
016| t0,t1 = 0.0, 10.0
017| sample_rate = 200.0
018|
019|
020| xs0 = np.arange(t0, t1, 1/sample_rate)
021| ys0 = [a*np.exp(-pow(x-b,2)/pow(2*c,2)) for x in xs0]
022|
023|
024| n = float(len(ys0))
025| half = int(n/2)
026| xs1 = np.arange(half)/(n/sample_rate)
027|
028| FT = np.fft.fft(ys0)/n
029| ys1 = [abs(c) for c in FT[range(half)]]
030|
031|
032| fig, ax = plt.subplots(2,1)
033| fig.suptitle('Fourier Transform - Time vs Frequency', fontsize=14, fontweight='bold')
034|
035|
036| ax[0].set_xlabel('time', fontsize=14)
037| ax[0].set_xlim(t0, t1)
038| ax[0].xaxis.set_major_locator(MultipleLocator(1.0))
039| ax[0].xaxis.set_minor_locator(MultipleLocator(0.25))
040| ax[0].xaxis.set_major_formatter(NullFormatter())
041|
042| ax[0].set_ylabel('energy', fontsize=14)
043| ax[0].set_ylim(-1.0, +1.0)
044| ax[0].yaxis.set_major_locator(MultipleLocator(1.0))
045| ax[0].yaxis.set_minor_locator(MultipleLocator(0.1))
046| ax[0].yaxis.set_major_formatter(NullFormatter())
047|
048| ax[0].grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
049| ax[0].grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
050| ax[0].tick_params(which='major', axis='both', direction='out', color='#000000')
051| ax[0].tick_params(which='minor', axis='both', direction='out', color='#000000')
052|
053| ax[0].plot(xs0,ys0, lw=1.0, c='#0000ff', zorder=11, alpha=0.80)
054|
055|
056| ax[1].set_xlabel('frequency', fontsize=14)
057| ax[1].set_xlim(0.0, sample_rate/2.0)
058| ax[1].xaxis.set_major_locator(MultipleLocator(10.0))
059| ax[1].xaxis.set_minor_locator(MultipleLocator(1.00))
060| ax[1].xaxis.set_major_formatter(FormatStrFormatter('%d'))
061|
062| ax[1].set_ylabel('energy', fontsize=14)
063| ax[1].set_ylim(0.0, +0.30)
064| ax[1].yaxis.set_major_locator(MultipleLocator(0.1))
065| ax[1].yaxis.set_minor_locator(MultipleLocator(0.02))
066| ax[1].yaxis.set_major_formatter(NullFormatter())
067|
068| ax[1].grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
069| ax[1].grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
070| ax[1].tick_params(which='major', axis='both', direction='out', color='#000000')
071| ax[1].tick_params(which='minor', axis='both', direction='out', color='#000000')
072|
073| ax[1].plot(xs1,ys1, lw=1.0, c='#ff0000', zorder=11, alpha=1.00)
074|
075|
076| fig.set_figwidth(8.0)
077| fig.set_figheight(5.0)
078| fig.subplots_adjust(left=0.05, right=0.98, bottom=0.11, top=0.93, hspace=0.25)
079|
080| fn = path.join(ImgsPath, Filename)
081| fig.savefig(fn)
082|
083|
084| '''eof'''
085|