fft-fake.html


001| ####################################################################################################
002| from sys import argv
003| from os import path
004| from math import pi, exp as rexp
005| from cmath import exp as cexp
006| import matplotlib.pyplot as plt
007| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
008| from goodies import divisions
009| ####################################################################################################
010| BasePath = r'C:\CJS\prj\Python\blog\proj'
011| ImgsPath = path.join(BasePath,'img')
012| Filename = 'fft-fake.png'
013| 
014| ##================================================================================================##
015| def main ():
016|     fig, ax = plt.subplots(2,1)
017|     fig.suptitle('Time vs Frequency', fontsize=14, fontweight='bold')
018| 
019|     ax[0].set_xlabel('time', fontsize=14)
020|     ax[0].set_xlim(-1.0, +1.0)
021|     ax[0].xaxis.set_major_locator(MultipleLocator(0.5))
022|     ax[0].xaxis.set_minor_locator(MultipleLocator(0.125))
023|     ax[0].xaxis.set_major_formatter(NullFormatter())
024| 
025|     ax[0].set_ylabel('energy', fontsize=14)
026|     ax[0].set_ylim(-1.1, +1.1)
027|     ax[0].yaxis.set_major_locator(MultipleLocator(1.0))
028|     ax[0].yaxis.set_minor_locator(MultipleLocator(0.1))
029|     ax[0].yaxis.set_major_formatter(NullFormatter())
030| 
031|     ax[0].grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
032|     ax[0].grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
033|     ax[0].tick_params(which='major', axis='both', direction='out', color='#000000')
034|     ax[0].tick_params(which='minor', axis='both', direction='out', color='#000000')
035| 
036|     K = lambda k: lambda x:cexp(complex(0,1)*complex(2.0*pi,0)*complex(x*k,0))
037| 
038|     def get_cycle_data (freq):
039|         F = K(freq)
040|         data = []
041|         for x in divisions(-1.0, +1.0, 800):
042|             t = (x, F(x))
043|             data.append(t)
044|         return data
045| 
046|     dat1 = get_cycle_data(13.0)
047|     dat2 = get_cycle_data(27.0)
048|     dat3 = get_cycle_data(42.0)
049| 
050|     xs0 = [d[0] for d in dat1]
051|     ys0 = [d[1].real for d in dat1]
052|     #ys0 = [(a[1].real+b[1].real+c[1].real)/3.0 for a,b,c in zip(dat1,dat2,dat3)]
053| 
054|     ax[0].plot(xs0,ys0, lw=1.4, c='#0000ff', zorder=11, alpha=0.80)
055| 
056|     ax[1].set_xlabel('frequency', fontsize=14)
057|     ax[1].set_xlim(0.0, +50.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, +1.1)
064|     ax[1].yaxis.set_major_locator(MultipleLocator(0.2))
065|     ax[1].yaxis.set_minor_locator(MultipleLocator(0.1))
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|     def get_peak_data (peak_height, peak_center, peak_width):
074|         F = lambda x: peak_height*rexp(-pow(x-peak_center,2)/pow(2*peak_width,2)) # (2*pow(C,2)) ?
075|         data = []
076|         for x in divisions(0.0,+50.0, 500):
077|             t = (x, F(x))
078|             data.append(t)
079|         return data
080| 
081|     dat1 = get_peak_data(1.00, 13.0, 0.25)
082|     dat2 = get_peak_data(1.00, 27.0, 0.25)
083|     dat3 = get_peak_data(1.00, 42.0, 0.25)
084| 
085|     xs1 = [d[0] for d in dat1]
086|     ys1 = [d[1] for d in dat1]
087|     #ys1 = [a[1]+b[1]+c[1] for a,b,c in zip(dat1,dat2,dat3)]
088| 
089|     ax[1].plot(xs1,ys1, lw=2.5, c='#ff0000', zorder=11, alpha=0.80)
090| 
091|     fig.set_figwidth(8.0)
092|     fig.set_figheight(5.0)
093|     fig.subplots_adjust(left=0.05, right=0.98, bottom=0.11, top=0.93, hspace=0.25)
094| 
095|     fn = path.join(ImgsPath, Filename)
096|     fig.savefig(fn)
097| 
098| ####################################################################################################
099| if __name__ == '__main__':
100|     print(argv[0])
101|     print()
102|     main()
103|     print()
104| ####################################################################################################
105| '''eof'''
106|