0
1

More than 1 year has passed since last update.

Pythonでの音声信号処理 (5) 色々な波形(ノコギリ波、矩形波)の作成

Last updated at Posted at 2023-01-09

やりたいこと

ノコギリ波、矩形波を作ってみること

やってみる

もう少しダミーデータ作成処理の検証も兼ねて、色々な波形として、ノコギリ波、矩形波を作成してみる。

ノコギリ波 = 1/1 sin x + 1/2 sin 2x + 1/3 sin 3x + ...

矩形波 = 1/1 sin x + 1/3 sin 3x + 1/5 sin 5x + ...
(ノコギリ波の奇数部分だけ)

p5.py
# -*- coding: utf-8 -*-
import sys
sys.dont_write_bytecode = True

from Grp import Line
import Dummy as dmy

from multiprocessing import Process
import time

def main():

    smpl = 1024  # サンプリングレートは1024
    sec = 2      # 2秒分

    # ノコギリ波
    wav1 = dmy.makeWave(1, smpl, sec, 0, 0, 1)
    for i in range(64):
        freq = i + 1
        wav1 += dmy.makeWave(freq, smpl, sec, 1 / freq, 0, 1)

    # 矩形波
    wav2 = dmy.makeWave(1, smpl, sec, 0, 0, 1)
    for i in range(64):
        freq = i + 1
        if freq % 2 == 0:
            continue
        wav2 += dmy.makeWave(freq, smpl, sec, 1 / freq, 0, 1)

    p_s = []

    p = Process(target=drawGraph, args=(wav1, smpl))
    p.start()
    p_s.append(p)

    p = Process(target=drawGraph, args=(wav2, smpl))
    p.start()
    p_s.append(p)

    while len(p_s) > 0:
        for p in p_s:
            if not p.is_alive():
                p.join()
                p_s.remove(p)
        time.sleep(0.1)

def drawGraph(data, rate):
    grp = Line()
    grp.setData(data, rate)
    grp.display()

if __name__ == '__main__':

    main()

実行結果

ノコギリ波
P05_1.png

矩形波
P05_2.png

補足

Grp.pyはここにあります。
https://qiita.com/u1tym/items/b52c466620f65225edf3

Dummy.pyはここにあります。
https://qiita.com/u1tym/items/3e2be5364e83df1b7792

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