1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonでフーリエ変換をやってみた(+α)

Last updated at Posted at 2021-06-01

はじめに

知見蓄積のために音声データを使ってフーリエ変換をやってみた。
ただフーリエ変換だけやっても面白くないので、データをNMRの結果に見立てて化学シフトも描画した。

環境

windows10
python:3.7.10
seaborn:0.11.1
matplotlib:3.2.2
pydub:0.25.1

##内容

1.フーリエ変換

#ファイルの読み込み
from google.colab import files
uploaded = files.upload()
#モジュール
!pip install pydub
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()
import numpy as np
from pydub import AudioSegment
%matplotlib inline

# 音声ファイルの読み込み
audio = AudioSegment.from_file('/content/Accent19-1.mp3', 'mp3')

# 情報の取得
time = audio.duration_seconds # 再生時間(秒)
rate = audio.frame_rate  # サンプリングレート(Hz)
audio = np.array(audio.get_array_of_samples()) #numpy配列に変換

# グラフ表示のための横軸を設定
o_time = np.arange(0, audio.shape[0]/rate,1/rate)

#プロット
plt.plot(o_time,audio)
plt.xlabel('time')
plt.ylabel('amplitude')

image.png

#x方向
number =audio.shape[0] #サンプル数
dt= time/number #時間幅
freq = np.fft.fftfreq(number,d=dt)

#y方向
fourier = np.fft.fft(audio) #フーリエ変換(時間⇒周波数へ)
amp = np.abs(fourier) #強度

plt.plot(freq,amp)
plt.xlabel('frequency(Hz)')
plt.ylabel('amplitude')

image.png

2.NMRの化学シフト

ここからは応用編。実際の未変換NMRデータはさすがに手に入らなかったので、音声データで代用。
正確なものではないが、なんとなくはイメージできるはず。
実際の測定では0~4000Hz程度になるので、いい感じにデータを分割しておく。

#右側だけ使う
fourier2 = fourier[:10000] 
amp2 = amp[:10000]
freq2 = freq[:10000]

plt.plot(freq2,amp2)
plt.xlabel('frequency(Hz)')
plt.ylabel('amplitude')

image.png

#NMRの結果に直す
amp2=amp2/10E+7 #割る数値は適当(それらしき値になるように)
base=500*10E+6 #測定周波数=500MHzと定義
ppm=((freq2+base)-base)/base*10E+6
plt.plot(ppm,amp2)
plt.xlabel('ppm')

image.png

まとめ

フーリエ変換から化学シフトまで実施できた。化学シフト計算は装置がやってくれるので、普段はやることないけど理解にはいいのでは。

参考

https://nehori.com/nikki/2020/12/06/post-22673/
https://algorithm.joho.info/programming/python/pydub-play/
http://www.pharm.tohoku.ac.jp/~henkan/lab/tanaka/lecture/OrgSpectrum_text.pdf

音声データは下記のサイトから。
https://otologic.jp/ 
image.png

1
4
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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?