2016年10月29日土曜日

Octaveレッスン(4) - 正弦波を生成する関数

* 正弦波を生成する関数

指定したサンプリングレート&周波数の正弦波を生成し、ファイルに保存する

---- gen_sin.m の中身 ----
function[y, t] = gen_sin(Fs, hertz, seconds)
  if(nargin == 0)
    disp("usage: gen_sin(Fs, hertz(, seconds))");
    return
  elseif(nargin < 3)
    seconds=1;
    fprintf('default seconds = %d\n', seconds)
  end

  t=[0:seconds*Fs-1]*1/Fs; %time vector
  y=sin(2*pi*hertz*t);
  file=input('output file:')
  if(sizeof(file)==0)
    disp('not output')
  else
    audiowrite(file, y, Fs)
  end
end
----------------

実行例1: サンプリングレート44.1k, 10Hzの正弦波を2秒生成し、ファイル保存し、描画
>> [y, t] = gen_sin(44100, 10, 2);
output file:'sin660Hz.wav'
file = sin660Hz.wav
>> plot(t,y)

実行例2: サンプリングレート44.1k, 2Hzの正弦波を2秒生成し、前のグラフに重ねて描画
>> [y2, t] = gen_sin(44100, 2, 2);
output file:
file = [](0x0)
not output file
>> hold on
>> plot(t,y2,'r-')
>> hold off

グラフ表示ウィンドウ上部の[Z+] でズーム、選択した領域をズームなど可能。

参考: 波形生成: 時間ベクトルと正弦波 オーディオ ファイルの書き込み

0 件のコメント:

コメントを投稿