SHARE
TWEET

Untitled

a guest May 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. 正弦波の足し合わせでのこぎり波を作成
  3. Usage:
  4. $ python fourier_sawtoothWave.py 100
  5. argv[1]は足し合わせる波の数を意味し,デフォルト10
  6. '''
  7.  
  8. import math
  9. import matplotlib.pyplot as plt
  10. import numpy as np
  11. import sys
  12.  
  13. def fourier_series(x, n):
  14.     '''
  15.     :param x: データx
  16.     :param n: 足し合わせる波の数
  17.     '''
  18.     data = []
  19.     for i in x: #xの各点についてyを計算
  20.         y=0.0
  21.         for j in range(1,n):
  22.             y += (2/j)*(-1)**(j+1)*np.sin(j*i) #のこぎり波のフーリエ級数
  23.         data.append(y)
  24.     return data
  25.  
  26. def main(n):
  27.     '''
  28.     :param n: 足し合わせる波の数
  29.     '''
  30.     pi = math.pi
  31.     x = np.arange(-2*pi, 2*pi, 0.1) #定義域(-2π~2π)
  32.     y = fourier_series(x, n) #yを計算
  33.     plt.plot(x,y)
  34.     plt.xticks([-pi,0,pi], ["-π","0","π"])
  35.     plt.yticks([-pi,0,pi], ["-π","0","π"])
  36.     plt.xlim([-2*pi,2*pi])
  37.     plt.show()
  38.  
  39. if __name__ == '__main__':
  40.     n = int(sys.argv[1]) if len(sys.argv)>1 else 10
  41.     main(n)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top