# only solution in this case is to avoid aliasing in the first place :) import numpy as np from pwtools import mpl from scipy.signal import hanning from scipy.fftpack import fft from pwtools.signal import fftsample, FIRFilter, pad_zeros pi = np.pi plt = mpl.plt plots = mpl.prepare_plots(["freq", "filt_pad", "filt_nopad"]) nyq = 100 # Hz df = 1.0 # Hz dt, nstep = fftsample(nyq, df, mode="f") t = np.linspace(0, 1, int(nstep)) filt1 = FIRFilter(cutoff=[10, 50], nyq=nyq, mode="bandpass", ripple=60, width=10) filt2 = FIRFilter(cutoff=[10, 50], nyq=nyq, mode="bandpass", ntaps=100, window="hamming") plots["freq"].ax.plot(filt1.w, abs(filt1.h), label="filt1") plots["freq"].ax.plot(filt2.w, abs(filt2.h), label="filt2") plots["freq"].ax.legend() for pad in [True, False]: x = np.sin(2 * pi * 20 * t) + np.sin(2 * pi * 80 * t) if pad: x = pad_zeros(x, nadd=len(x)) pl = plots["filt_pad"] else: pl = plots["filt_nopad"] f = np.fft.fftfreq(len(x), dt)
print("not found, will skip fourier.x") # For the calculation of nstep and dt, we increase `fmax` to # fmax*fmax_extend_fac to lower dt. That way, the signal still contains # frequencies up to `fmax`, but the FFT frequency axis does not end at exactly # `fmax` but extends to higher values (we increase the Nyquist frequency). df = 0.001 fmax = 1 fmax_extend_fac = 1.5 # number of frequencies contained in the signal nfreq = 10 # time axis that assures that the fft catches all freqs up to and beyond fmax # to avoid FFT aliasing dt, nstep = signal.fftsample(fmax * fmax_extend_fac, df) nstep = int(nstep) taxis = np.linspace(0, dt * nstep, nstep, endpoint=False) ##print "dt [s]:", dt ##print "dt [Hartree]:", dt/constants.th ##print "nstep:", nstep ##print "fmax [Hz]:", fmax ##print "nfreq:", nfreq ############################################################################### # 1d arrays ############################################################################### # First, we treat a simple 1d signal to test the math.
# and just using a bandpass around you desired frequency band won't help. The # only solution in this case is to avoid aliasing in the first place :) import numpy as np from pwtools import mpl from scipy.signal import hanning from scipy.fftpack import fft from pwtools.signal import fftsample, FIRFilter, pad_zeros pi = np.pi plt = mpl.plt plots = mpl.prepare_plots(['freq', 'filt_pad', 'filt_nopad']) nyq = 100 # Hz df = 1.0 # Hz dt, nstep = fftsample(nyq, df, mode='f') t = np.linspace(0, 1, int(nstep)) filt1 = FIRFilter(cutoff=[10, 50], nyq=nyq, mode='bandpass', ripple=60, width=10) filt2 = FIRFilter(cutoff=[10, 50], nyq=nyq, mode='bandpass', ntaps=100, window='hamming') plots['freq'].ax.plot(filt1.w, abs(filt1.h), label='filt1') plots['freq'].ax.plot(filt2.w, abs(filt2.h), label='filt2') plots['freq'].ax.legend()
print "not found, will skip fourier.x" # For the calculation of nstep and dt, we increase `fmax` to # fmax*fmax_extend_fac to lower dt. That way, the signal still contains # frequencies up to `fmax`, but the FFT frequency axis does not end at exactly # `fmax` but extends to higher values (we increase the Nyquist frequency). df = 0.001 fmax = 1 fmax_extend_fac = 1.5 # number of frequencies contained in the signal nfreq = 10 # time axis that assures that the fft catches all freqs up to and beyond fmax # to avoid FFT aliasing dt, nstep = signal.fftsample(fmax*fmax_extend_fac, df) nstep = int(nstep) taxis = np.linspace(0, dt*nstep, nstep, endpoint=False) ##print "dt [s]:", dt ##print "dt [Hartree]:", dt/constants.th ##print "nstep:", nstep ##print "fmax [Hz]:", fmax ##print "nfreq:", nfreq ############################################################################### # 1d arrays ############################################################################### # First, we treat a simple 1d signal to test the math.