Last active
March 9, 2019 12:22
-
-
Save wakusei-meron-/7de49d2c080ffd0420a08450bcda7a94 to your computer and use it in GitHub Desktop.
Desktop/PPP/python/audio/sin.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "\"\"\"任意の波の生成\"\"\"\n\n# 振幅, 周波数, 位相, 時間\n# y = Asin(ωt + φ)\n\nimport numpy as np\n\nsr = 44100 # サンプリング周波数\nfrq = 440 # 波形の周波数\n\ndef genSinWav(amp, frq, sr, t, quant_byte_type):\n\n # サンプリング周波数毎のフレームの作成 \n rad = np.linspace(0, 2*np.pi * frq * t, sr * t)\n \n # sin波の作成\n y1 = np.sin(rad) * amp\n \n # 量子化\n y2 = (y1 * np.iinfo(quant_byte_type).max).astype(np.int16)\n \n # ステレオにする\n return np.array(np.c_[y2,y2])\n\ngenSinWav(0.5, 440, 44100, 2, np.int16)", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 1, | |
"data": { | |
"text/plain": "array([[ 0, 0],\n [ 1026, 1026],\n [ 2048, 2048],\n ...,\n [-2048, -2048],\n [-1026, -1026],\n [ 0, 0]], dtype=int16)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import scipy.io.wavfile as siw\n# 作成した波を保存\nw = genSinWav(0.5, frq, sr, 2, np.int16)\nsiw.write(\"sin_440Hz.wav\", sr, w)", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.3", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "7de49d2c080ffd0420a08450bcda7a94", | |
"data": { | |
"description": "Desktop/PPP/python/audio/sin.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/7de49d2c080ffd0420a08450bcda7a94" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment