LoginSignup
2
0

More than 3 years have passed since last update.

sin波を作る

Last updated at Posted at 2021-02-11
sin.py

import wave
import numpy as np
from matplotlib import pylab as plt
import struct

a = 1     #振幅
fs = 44100 #サンプリング周波数
f0 = 440  #周波数(ラの音にあたります)
sec = 5   #秒
voice=[]

#サイン波を生成
for n in np.arange(fs * sec):
    s = a * np.sin(2.0 * np.pi * f0 * n / fs)
    voice.append(s)

#tは時間方向での離散値.voiceの長さをサンプルレートで切っているので,サンプル数分の配列になってます.
t = np.arange(0, len(voice))/fs

#サイン波を表示
plt.plot(t,voice)
plt.xlim([0,0.01])#範囲指定0秒〜0.01秒
plt.show()

実行は以下のコマンドで行います.

$ python sin.py

出力結果

image.png

440Hzは 1秒間に440回振動しているという意味なので,
0.01秒間に4.4回振動しますね.

さて,周波数を変えるとどんな関数になるでしょう?

参考
https://qiita.com/MuAuan/items/ef4da6167d13cbf56e78

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0