Subsections

音生成 / Synthesizers

インパルス / Impulse

function out = synth_impulse(ms, fs)
%SYNTH_IMPULSE   generate impulse
%   out = synth_impulse(ms, fs) generate a vector having one impulse at
%   the beginning.
%
%   ms - length of signal in milli-seconds
%   fs - sampling frequency in Hz
%
% 2004-11-05 by MARUI Atsushi

N = ceil(ms / 1000 * fs);
out = [1 zeros(1, N-1)];

正弦波 / Sinewave

function out = synth_sinewave(freq, ms, fs)
%SYNTH_SINEWAVE   Sine wave generator.
%   synth_sinewave(freq, ms, fs) generates a sinusoidal wave of
%   specified pitch and duration with unity amplitude.
%
%   freq - frequency of the tone in Hertz
%     ms - duration in milli-second
%     fs - sampling frequency in Hertz
%
%   2002-10-30 by MARUI Atsushi

out = zeros(ceil(fs*ms/1000), 1);

c = 2 * pi * (freq / fs);
i=[1:size(out, 1)];
out(i) = sin(c * (i-1));
out = normalize(out);

鋸歯波 / Sawwave

function out = synth_sawwave(freq, ms, fs)
%SYNTH_SAWWAVE   Saw wave generator.
%   synth_sawwave(freq, ms, fs) generates a saw wave of specified
%   pitch and duration with unity amplitude.
%
%   freq - frequency of the tone in Hertz
%     ms - duration in milli-second
%     fs - sampling frequency in Hertz
%
%   2003-08-13 by MARUI Atsushi
%   2004-01-28 added direct method

method = 'build-up';
%method = 'direct';

switch lower(method)
  case {'build-up'}
    out = zeros(ceil(fs*ms/1000), 1);
    R = floor(fs/2 / freq); %number of sinewaves in a sawwave
    c = 2 * pi * (freq / fs);
    i = [1:size(out, 1)];
    for k=1:R
      out(i) = out(i) + (sin(c * (i-1) * k)/k)';
    endfor
  case {'direct'}
    t = 0:1/fs:ms/1000;
    out = sawtooth(2*pi*freq*t);
endswitch
out = normalize(out);

矩形波 / Squarewave

function out = synth_squarewave(freq, ms, fs)
%SYNTH_SQUAREWAVE   Square wave generator.
%   synth_squarewave(freq, ms, fs) generates a square wave of
%   specified pitch and duration with unity amplitude.
%
%   freq - frequency of the tone in Hertz`
%     ms - duration in milli-second
%     fs - sampling frequency in Hertz
%
%   2004-01-10 by MARUI Atsushi
%   2004-10-30 added direct method

method = 'build-up';
%method = 'direct';

switch lower(method)
  case {'build-up'}
    out = zeros(ceil(fs*ms/1000), 1);
    R = floor(fs/2 / freq); %number of sinewaves in a squarewave    
    c = 2 * pi * (freq / fs);
    i = [1:size(out, 1)];
    for k=1:2:R
      out(i) = out(i) + (sin(c * (i-1) * k)/k)';
    end
  case {'direct'}
    t = 0:1/fs:ms/1000;
    out = square(2*pi*freq*t);
end
out = normalize(out);

スウィープ / Sweeped Sinewave

function y = synth_sinesweep(ms, fs)
%SYNTH_SINESWEEP   generate sinusoidal sweep (chirp) signal
%   synth_sinesweep(ms, fs) generates a sweeping sinusoidal wave from
%   DC to Nyquist of specified duration with unity amplitude.
%
%   Input:
%      ms - duration in milli-second
%      fs - sampling frequency in Hertz
%
%   Example:
%      y = synth_sinesweep(1000, 44100);
%      sound(y, 44100);
%      specgram(y, 256, 44100);
%
%   2004-11-11 by MARUI Atsushi

method = 'logarithmic';

t = 0 : 1/fs : ms/1000;
t = t(1:length(t)-1);
y = chirp(t, min(t), max(t), fs/2, method);

白色雑音 / Whitenoise

function out = synth_whitenoise(ms, fs)
%SYNTH_WHITENOISE   Whitenoise Generator
%   synth_whitenoise(ms, fs) generates a Gaussian whitenoise of
%   specified duration with unity amplitude at the maximum peak.
%
%   ms - duration of noise in milli-seconds
%   fs - sampling frequency in Hz
%
%   2003-01-03 by MARUI Atsushi
%   2003-02-04 Fixed a bug so that it generates noise in proper range.
%   2004-09-15 Fixed to use randn instead of rand
%              (now a valid Gaussian white noise)

out = normalize(randn(ceil(fs*ms/1000), 1));



MARUI Atsushi
2023-12-05