Lowpass filter with a time-varying cutoff frequency, with Python

前端 未结 2 1671
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 14:16

How to apply a lowpass filter, with cutoff frequency varying linearly (or with a more general curve than linear) from e.g. 10000hz to 200hz along time, with

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 15:03

    you can use scipy.fftpack.fftfreq and scipy.fftpack.rfft to set thresholds

    fft = scipy.fftpack.fft(sound)
    freqs = scipy.fftpack.fftfreq(sound.size, time_step)
    

    for the time_step I did twice the sampling rate of the sound

    fft[(freqs < 200)] = 0
    

    this would set all set all frequencies less than 200 hz to zero

    for the time varying cut off, I'd split the sound and apply the filters then. assuming the sound has a sampling rate of 44100, the 5000hz filter would start at sample 220500 (five seconds in)

    10ksound = sound[:220500]
    10kfreq = scipy.fftpack.fftreq(10ksound.size, time_step)
    10kfft = scipy.fftpack.fft(10ksound)
    10kfft[(10kfreqs < 10000)] = 0
    

    then for the next filter:

    5ksound = sound[220500:396900]
    5kfreq = scipy.fftpack.fftreq(10ksound.size, time_step)
    5kfft = scipy.fftpack.fft(10ksound)
    5kfft[(5kfreqs < 5000)] = 0
    

    etc

    edit: to make it "sliding" or a gradual filter instead of piece wise, you could make the "pieces" much smaller and apply increasingly bigger frequency thresholds to the corresponding piece(5000 -> 5001 -> 5002)

提交回复
热议问题