def fast_conv_vect(input_signal, impulse): # FFT를 수행하는 데 필요한 포인트의 양을 찾아 벡터화 합니다. length = size(impulse) + size(input_signal) - 1 # 선형 컨벌루션 길이 next_pow = nextpow2(length) # 곱셈 결과의 IDFT가 순환 컨벌루션이기에 공통적으로 일치시키기 위해서는 N>=L을 충족해야만 합니다. # (여기서 L=N1+N2-1;N1=length(input_signal);N2=length(impulse)) # fft(x, n)는 n개의 포인트를 가지는 FFT입니다. x가 n보다 적으면 남는 부분을 0으로 채우고, 더 많은 경우에는 잘립니다. spectral_m = fft(impulse, next_pow) * fft( input_signal, next_pow) # 임펄스와 입력 신호를 FFT하여 곱합니다. return inverse_fft(spectral_m) # 타임 도메인 재계산 후 반환.
def main(): N = 3000 x = Numeric.arange(N,typecode=Numeric.Float)*0.01 y = Numeric.sin(3.0*x) + 4*Numeric.cos(x) # Calculate the FFT H = fft(y) h = fft(H).real / N psd = (H*Numeric.conjugate(y)).real / N dt = x[1]-x[0] # Write FFT_inverse to file out = open('fft.dat','w') for i in range(len(x)): out.write(str(x[i])+' '+str(y[i])+' '+str(psd[i])+' '+str(h[len(h)-i-1])+'\n') out.close()
def main(): N = 3000 x = Numeric.arange(N, typecode=Numeric.Float) * 0.01 y = Numeric.sin(3.0 * x) + 4 * Numeric.cos(x) # Calculate the FFT H = fft(y) h = fft(H).real / N psd = (H * Numeric.conjugate(y)).real / N dt = x[1] - x[0] # Write FFT_inverse to file out = open('fft.dat', 'w') for i in range(len(x)): out.write( str(x[i]) + ' ' + str(y[i]) + ' ' + str(psd[i]) + ' ' + str(h[len(h) - i - 1]) + '\n') out.close()
def psd(t, y): """ Calculate and return frequencies and PSD of y(t) """ N = float(len(t)) # GET # OF DATA POINTS dt = t[1] - t[0] # GET TIME INTERVAL T = N * dt # DEFINE THE PERIOD (TOTAL TIME) df = 1.0 / T # DEFINE FREQUENCY STEP H = fft(y) # ,n=256) ADDITIONAL OPTION # Caculate frequencies and PSD f = Numeric.arange(N, typecode=Numeric.Float) * df PSD = (Numeric.conjugate(H) * H).real / N return f, PSD
def psd(t,y): """ Calculate and return frequencies and PSD of y(t) """ N = float(len(t)) # GET # OF DATA POINTS dt = t[1] - t[0] # GET TIME INTERVAL T = N * dt # DEFINE THE PERIOD (TOTAL TIME) df = 1.0 / T # DEFINE FREQUENCY STEP H = fft(y) # ,n=256) ADDITIONAL OPTION # Caculate frequencies and PSD f = Numeric.arange(N,typecode=Numeric.Float)*df PSD = (Numeric.conjugate(H)*H).real / N return f, PSD
from doppler import v_receiver ''' By Shao-Hsuan (Ernie) Chu, NSYSU, Taiwan Default record file name: record.wav Default source file name: 244hz.wav ''' recordFile = input('Record file: ') sourceFile = input('Souce file: ') sample_rate, timeDomain = wavfile.read(recordFile) plt.subplot(221) plt.plot(range(len(timeDomain)), timeDomain) plt.xlabel('(s)') plt.title('Original wave with v') freqDomain = fft(timeDomain) freqData = abs(freqDomain) / len(freqDomain) freqData = freqData[range(3000)] fileLength = len(freqDomain) / sample_rate x = np.linspace(0, 3000 / fileLength, 3000) plt.subplot(222) plt.plot(x, freqData) plt.xlabel('(Hz)') plt.title('FFT of the wave(part)') sample_rate_single, timeDomain_single = wavfile.read(sourceFile) plt.subplot(223) plt.plot(range(len(timeDomain_single)), timeDomain_single, 'g') plt.xlabel('(s)')
def psd(x, NFFT=256, Fs=2, detrend=detrend_none, window=window_hamming, noverlap=0): """ The power spectral density by Welches average periodogram method. The vector x is divided into NFFT length segments. Each segment is detrended by function detrend and windowed by function window. noperlap gives the length of the overlap between segments. The absolute(fft(segment))**2 of each segment are averaged to compute Pxx, with a scaling to correct for power loss due to windowing. Fs is the sampling frequency. -- NFFT must be a power of 2 -- detrend and window are functions, unlike in matlab where they are vectors. -- if length x < NFFT, it will be zero padded to NFFT Refs: Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) """ if NFFT % 2: raise ValueError, 'NFFT must be a power of 2' # zero pad x up to NFFT if it is shorter than NFFT if len(x) < NFFT: n = len(x) x = resize(x, (NFFT, )) x[n:] = 0 # for real x, ignore the negative frequencies # if x.typecode()==Complex: numFreqs = NFFT if any(numpy.iscomplex(x)): numFreqs = NFFT else: numFreqs = NFFT // 2 + 1 # windowVals = window(ones((NFFT,),x.typecode())) windowVals = window(numpy.ones(NFFT)) step = NFFT - noverlap ind = range(0, len(x) - NFFT + 1, step) n = len(ind) # Pxx = zeros((numFreqs,n), Float) Pxx = numpy.zeros([numFreqs, n]) # do the ffts of the slices for i in range(n): thisX = x[ind[i]:ind[i] + NFFT] thisX = windowVals * detrend(thisX) fx = absolute(fft(thisX))**2 #print("numFreqs={0:f}".format(numFreqs)) #print("len of fx slice={0:d}".format(len(fx[:int(numFreqs)]))) #print("len of destination in Pxx={0:d}") Pxx[:, i] = fx[:int(numFreqs)] # Scale the spectrum by the norm of the window to compensate for # windowing loss; see Bendat & Piersol Sec 11.5.2 if n > 1: Pxx = mean(Pxx, 1) Pxx = divide(Pxx, norm(windowVals)**2) freqs = Fs / NFFT * arange(0, numFreqs) return Pxx, freqs
def csd(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hamming, noverlap=0): """ The cross spectral density Pxy by Welches average periodogram method. The vectors x and y are divided into NFFT length segments. Each segment is detrended by function detrend and windowed by function window. noverlap gives the length of the overlap between segments. The product of the direct FFTs of x and y are averaged over each segment to compute Pxy, with a scaling to correct for power loss due to windowing. Fs is the sampling frequency. NFFT must be a power of 2 Refs: Bendat & Piersol -- Random Data: Analysis and Measurement Procedures, John Wiley & Sons (1986) """ if NFFT % 2: raise ValueError, 'NFFT must be a power of 2' # zero pad x and y up to NFFT if they are shorter than NFFT if len(x) < NFFT: n = len(x) x = resize(x, (NFFT, )) x[n:] = 0 if len(y) < NFFT: n = len(y) y = resize(y, (NFFT, )) y[n:] = 0 # for real x, ignore the negative frequencies # if x.typecode()==Complex: numFreqs = NFFT if any(numpy.iscomplex(x)): numFreqs = NFFT else: numFreqs = NFFT // 2 + 1 # windowVals = window(ones((NFFT,),x.typecode())) windowVals = window(numpy.ones(NFFT)) step = NFFT - noverlap ind = range(0, len(x) - NFFT + 1, step) n = len(ind) # Pxy = zeros((numFreqs,n), Complex) Pxy = numpy.zeros([numFreqs, n]) # do the ffts of the slices for i in range(n): thisX = x[ind[i]:ind[i] + NFFT] thisX = windowVals * detrend(thisX) thisY = y[ind[i]:ind[i] + NFFT] thisY = windowVals * detrend(thisY) fx = fft(thisX) fy = fft(thisY) Pxy[:, i] = fy[:numFreqs] * conjugate(fx[:numFreqs]) # Scale the spectrum by the norm of the window to compensate for # windowing loss; see Bendat & Piersol Sec 11.5.2 if n > 1: Pxy = mean(Pxy, 1) Pxy = divide(Pxy, norm(windowVals)**2) freqs = Fs / NFFT * arange(0, numFreqs) return Pxy, freqs
def spectrum(complex_list): """calculates the spectrum of all elements in the list""" return abs(fft(complex_list,1024))