信号生成 / Signal Generation

def synth_sinewave(freq=1000, duration=1.0, samprate=44100):
    t = np.arange(0, duration*samprate) / samprate
    x = np.sin(2*math.pi * freq * t)
    return(x / np.max(np.abs(x)), t)

def synth_sawwave(freq=1000, duration=1.0, samprate=44100):
    t = np.arange(0, duration*samprate) / samprate
    x = np.zeros(len(t))
    R = math.floor(samprate/2 / freq)
    for k in range(R):
        x = x + np.sin(2*math.pi * (k+1) * freq * t) / (k+1)
    return(x / np.max(np.abs(x)), t)

def synth_squarewave(freq=1000, duration=1.0, samprate=44100):
    t = np.arange(0, duration*samprate) / samprate
    x = np.zeros(len(t))
    R = math.floor(samprate/2 / freq)
    for k in range(0, R, 2):
        x = x + np.sin(2*math.pi * (k+1) * freq * t) / (k+1)
    return(x / np.max(np.abs(x)), t)

def synth_whitenoise(duration=1.0, samprate=44100):
    """
    generate whitenoise using normal distribution
    """
    t = np.arange(start=0, stop=duration, step=1/samprate)
    x = np.random.normal(size=len(t))
    return(x / max(abs(x)), t)

def synth_whitenoise_uniform(duration=1.0, samprate=44100):
    """
    generate whitenoise using uniform distribution
    """
    t = np.arange(start=0, stop=duration, step=1/samprate)
    x = np.random.uniform(size=len(t)) * 2.0 - 1.0
    return(x / max(abs(x)), t)

def synth_pinknoise(duration=1.0, samprate=44100):
    """
    Generate pinknoise using Voss algorithm
    http://www.firstpr.com.au/dsp/pink-noise/
    """
    f_low = 10   # lowest frequency to keep pink (Hz)
    levels = math.ceil(np.log2(samprate/f_low))
    t = np.arange(start=0, stop=duration, step=1/samprate)
    out = np.zeros(len(t))
    x = np.random.normal(size=levels)
    for n in range(math.ceil(samprate * duration)):
        for m in range(levels):
            if n % 2**(m+1) == 0:
                x[m] = np.random.normal()
            out[n] = np.random.normal() + np.sum(x)
    return(out / max(abs(out)), t)



MARUI Atsushi
2025-04-15