fft-1.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 #path.join(BasePath,'img')
010| Filename = 'fourier-1.png'
011| 
012| f1 = float(argv[1]) if 1 < len(argv) else 4.2
013| f2 = float(argv[2]) if 2 < len(argv) else 13.4
014| f3 = float(argv[2]) if 2 < len(argv) else 21.9
015| 
016| t0,t1 = 0.0, 10.0
017| sample_rate = 100.0
018| 
019| # Sine waves time base...
020| xs0 = np.arange(t0, t1, 1/sample_rate)
021| # Sine waves...
022| s1 = np.sin(xs0 * (2 * np.pi * f1))
023| s2 = np.sin(xs0 * (2 * np.pi * f2))
024| s3 = np.sin(xs0 * (2 * np.pi * f3))
025| s0 = (s1 + s2 + s3)/3.0
026| n = float(len(s0))
027| 
028| # FFT...
029| half = int(n/2)
030| xs1 = np.arange(half)/(n/sample_rate)
031| 
032| FT = np.fft.fft(s0)/n
033| ys1 = [abs(c) for c in FT[range(half)]]
034| 
035| ##------------------------------------------------------------------------------------------------##
036| fig, ax = plt.subplots(2,1)
037| fig.suptitle('Fourier Transform - Time vs Frequency', fontsize=14, fontweight='bold')
038| 
039| ##------------------------------------------------------------------------------------------------##
040| ax[0].set_xlabel('time', fontsize=14)
041| ax[0].set_xlim(t0, t1/2)
042| ax[0].xaxis.set_major_locator(MultipleLocator(1.0))
043| ax[0].xaxis.set_minor_locator(MultipleLocator(0.25))
044| ax[0].xaxis.set_major_formatter(NullFormatter())
045| 
046| ax[0].set_ylabel('energy', fontsize=14)
047| ax[0].set_ylim(-1.0, +1.0)
048| ax[0].yaxis.set_major_locator(MultipleLocator(1.0))
049| ax[0].yaxis.set_minor_locator(MultipleLocator(0.1))
050| ax[0].yaxis.set_major_formatter(NullFormatter())
051| 
052| ax[0].grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
053| ax[0].grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
054| ax[0].tick_params(which='major', axis='both', direction='out', color='#000000')
055| ax[0].tick_params(which='minor', axis='both', direction='out', color='#000000')
056| 
057| ax[0].plot(xs0,s0, lw=1.0, c='#0000ff', zorder=11, alpha=0.80)
058| 
059| ##------------------------------------------------------------------------------------------------##
060| ax[1].set_xlabel('frequency', fontsize=14)
061| ax[1].set_xlim(0.0, sample_rate/2.0)
062| ax[1].xaxis.set_major_locator(MultipleLocator(10.0))
063| ax[1].xaxis.set_minor_locator(MultipleLocator(1.00))
064| ax[1].xaxis.set_major_formatter(FormatStrFormatter('%d'))
065| 
066| ax[1].set_ylabel('energy', fontsize=14)
067| ax[1].set_ylim(0.0, +0.30)
068| ax[1].yaxis.set_major_locator(MultipleLocator(0.1))
069| ax[1].yaxis.set_minor_locator(MultipleLocator(0.02))
070| ax[1].yaxis.set_major_formatter(NullFormatter())
071| 
072| ax[1].grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
073| ax[1].grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
074| ax[1].tick_params(which='major', axis='both', direction='out', color='#000000')
075| ax[1].tick_params(which='minor', axis='both', direction='out', color='#000000')
076| 
077| ax[1].plot(xs1,ys1, lw=1.0, c='#ff0000', zorder=11, alpha=1.00)
078| 
079| ##------------------------------------------------------------------------------------------------##
080| fig.set_figwidth(8.0)
081| fig.set_figheight(5.0)
082| fig.subplots_adjust(left=0.05, right=0.98, bottom=0.11, top=0.93, hspace=0.25)
083| 
084| fn = path.join(ImgsPath, Filename)
085| fig.savefig(fn)
086| 
087| ####################################################################################################
088| '''eof'''
089|