def iff_filter(sig, scale, plot_show = 0): order = max(sig.size*scale,90) #order = 80 # Extend signal on both sides for removing boundary effect in convolution sig_extend = np.ones(sig.size+int(order/2)*2) sig_extend[int(order/2):(sig.size+int(order/2))] = sig sig_extend[0:int(order/2)] = sig[(sig.size-int(order/2)):sig.size] sig_extend[(sig.size+int(order/2)):sig_extend.size] = sig[0:int(order/2)] # convolve with hamming window and normalize smooth_sig = np.convolve(sig_extend,np.hamming(order),'same') smooth_sig = smooth_sig[int(order/2):(sig.size+int(order/2))] smooth_sig = np.amax(sig)/np.amax(smooth_sig)*smooth_sig # Plot signal for debug if(plot_show == 1): fig, ax = plt.subplots(ncols=2) ax[0].plot(sig) ax[0].plot(smooth_sig,'-r') ax[0].plot(med_sig,'black') ax[1].loglog(rfft(sig)) ax[1].loglog(rfft(smooth_sig),'-r') ax[1].loglog(rfft(med_sig),'black') plt.show() return smooth_sig
def subtract_original_signal_from_picked_signal(self, original_signal, picked_signal): # Note this function assumes that the signals are aligned for the starting point! fft_length = max(len(original_signal), len(picked_signal)) original_f_domain = rfft(original_signal, n= fft_length) picked_f_domain = rfft(picked_signal, n= fft_length) assert len(original_f_domain) == len(picked_f_domain) difference_signal = picked_f_domain - original_f_domain return irfft(difference_signal)
def test_random_real(self): for size in [1, 51, 111, 100, 200, 64, 128, 256, 1024]: x = random([size]).astype(self.rdt) y1 = irfft(rfft(x)) y2 = rfft(irfft(x)) assert_equal(y1.dtype, self.rdt) assert_equal(y2.dtype, self.rdt) assert_array_almost_equal(y1, x, decimal=self.ndec, err_msg="size=%d" % size) assert_array_almost_equal(y2, x, decimal=self.ndec, err_msg="size=%d" % size)
def test_definition(self): x = [1,2,3,4,1,2,3,4] y = rfft(x) y1 = direct_rdft(x) assert_array_almost_equal(y,y1) x = [1,2,3,4,1,2,3,4,5] y = rfft(x) y1 = direct_rdft(x) assert_array_almost_equal(y,y1)
def test_random_real(self): for size in [1,51,111,100,200,64,128,256,1024]: x = random([size]).astype(self.rdt) y1 = irfft(rfft(x)) y2 = rfft(irfft(x)) self.failUnless(y1.dtype == self.rdt, "Output dtype is %s, expected %s" % (y1.dtype, self.rdt)) self.failUnless(y2.dtype == self.rdt, "Output dtype is %s, expected %s" % (y2.dtype, self.rdt)) assert_array_almost_equal (y1, x, decimal=self.ndec) assert_array_almost_equal (y2, x, decimal=self.ndec)
def test_size_accuracy(self): # Sanity check for the accuracy for prime and non-prime sized inputs if self.rdt == np.float32: rtol = 1e-5 elif self.rdt == np.float64: rtol = 1e-10 for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: np.random.seed(1234) x = np.random.rand(size).astype(self.rdt) y = irfft(rfft(x)) _assert_close_in_norm(x, y, rtol, size, self.rdt) y = rfft(irfft(x)) _assert_close_in_norm(x, y, rtol, size, self.rdt)
def bandpass_filter(par, data, init_dates, leads, continuous_data=False, hi=20., lo=100., returnmeans=False): from scipy.fftpack import rfft, irfft, fftfreq assert(len(init_dates))==np.shape(data)[0] if continuous_data: w_min = 1./lo w_max = 1./hi dt = (init_dates[1]-init_dates[0]).days w = fftfreq(np.shape(data)[0], d=dt) data_hat = rfft(data, axis=0) data_hat[(w<w_min),:] = 0 # low pass data_hat[(w>w_max),:] = 0 # high pass data = irfft(data_hat, axis=0) else: # ASSUME data is already weekly-averaged, so simply subtract the anomaly # from the past 120d to filter out the low frequencies meanfile = '/home/disk/vader2/njweber2/research/subseasonal/data/' + \ 'all_forecasts/{}_120dMEANS_1982-2008.nc'.format(par) with Dataset(meanfile, 'r') as ncdata: mdates = ncdata.variables['init_dates'] mdates = num2date(mdates[:], mdates.units) mleads = ncdata.variables['ftime'][:] l_inds = np.array([np.where(mleads==l)[0][0] for l in leads]) means = ncdata.variables['forecasts'][:,l_inds, :, :] if len(leads)>1: assert np.shape(means)[1]==len(leads) if not returnmeans: assert(len(leads))==np.shape(data)[1] assert np.shape(data)==np.shape(means) data -= means if returnmeans: return means.squeeze() else: return data
def test_definition(self): for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]: x = np.array(t, dtype=self.rdt) y = rfft(x) y1 = direct_rdft(x) assert_array_almost_equal(y, y1) assert_equal(y.dtype, self.rdt)
def compute_fft(fs, ir): import scipy.signal, numpy from scipy import fftpack # creating asymmetric bartlett window for spectral analysis window_bart = scipy.signal.bartlett(len(ir), sym=False) # windowing the impulse response ir_wind = ir * window_bart # computing fft sig_fft = fftpack.rfft(ir_wind) # setting length of fft n = sig_fft.size timestep = 1 / float(fs) # generating frequencies according to fft points freq = fftpack.rfftfreq(n, d=timestep) # normalizing fft sys_fft = abs(sig_fft) / n # TODO FFT computing return sys_fft, freq
def getFrequency(price_values): #Piazza Code yhat = fftpack.rfft(price_values) idx = (yhat[1:]**2).argmax() + 1 freqs = fftpack.rfftfreq(len(price_values), d=(1.0) / (2 * np.pi)) frequency = freqs[idx] return frequency
def ApplySpecialFilter(inputs, filter_feature, reshaped): print("--Apply special filter to the inputs--") result = [] for single_input in inputs: epoch = [] for j in range(8): input_fft = rfft(single_input[j]) filter_fft = rfft(filter_feature[j]) output = irfft(input_fft - filter_fft) epoch.append(output) if reshaped: result.append(np.array(epoch).reshape(-1)) else: result.append(np.array(epoch)) print("--Done--") return np.array(result)
def sineFit(self,xReal,yReal): N=len(xReal) OFFSET = (yReal.max()+yReal.min())/2. yhat = fftpack.rfft(yReal-OFFSET) idx = (yhat**2).argmax() freqs = fftpack.rfftfreq(N, d = (xReal[1]-xReal[0])/(2*np.pi)) frequency = freqs[idx]/(2*np.pi) #Convert angular velocity to freq amplitude = (yReal.max()-yReal.min())/2.0 phase=0#.5*np.pi*((yReal[0]-offset)/amplitude) guess = [amplitude, frequency, phase,0] try: (amplitude, frequency, phase,offset), pcov = optimize.curve_fit(self.sineFunc, xReal, yReal-OFFSET, guess) offset+=OFFSET ph = ((phase)*180/(np.pi)) if(frequency<0): #print 'negative frq' return False if(amplitude<0): ph-=180 if(ph<0):ph = (ph+720)%360 freq=1e6*abs(frequency) amp=abs(amplitude) pcov[0]*=1e6 #print pcov if(abs(pcov[-1][0])>1e-6): False return [amp, freq, offset,ph] except: return False
def topPeaks(sound): fourier = abs(rfft(sound)) phase = (np.angle(rfft(sound))) indices = peakutils.indexes(fourier, thres=0.02 / max(fourier), min_dist=0.1) amplitudes = (fourier[indices]) amplitudes = (np.sort(amplitudes))[len(amplitudes) - 3:len(amplitudes)] a = fourier.tolist().index(amplitudes[0]) b = fourier.tolist().index(amplitudes[1]) c = fourier.tolist().index(amplitudes[2]) d, e, f = amplitudes g = phase[a] h = phase[b] i = phase[c] return a, b, c, d, e, f, g, h, i
def fit_rabi_t(xdata, ydata, f_pulse, scaling=True, offset=True, **kwargs): from scipy.fftpack import rfft, rfftfreq # ordering = xdata.argsort() ordering = argsort(xdata).values yhat = rfft(ydata[ordering]) idx = (yhat**2).argmax() freqs = rfftfreq(len(xdata), d=xdata[ordering].diff()[1]) #fR_guess = 0.7*freqs[idx] fR_guess = 2 / max(xdata) # assuming roughly one Rabi period of data # fR_guess = 1/(19e-6) f0_guess = f_pulse params_guess = [f0_guess, fR_guess] # print params_guess A_guess = 1 if scaling * 0: params_guess.append(A_guess) if scaling and offset: c_guess = 0 params_guess.append(c_guess) def fitfn(t, *pars): return rabi(t, f_pulse, *pars) params, u_params = curve_fit(fitfn, xdata, ydata, params_guess, **kwargs) params[1] = abs(params[1]) return params, u_params
def fft_filter(x, fs, band=(9, 14)): w = fftfreq(x.shape[0], d=1. / fs * 2) f_signal = rfft(x, axis=0) cut_f_signal = f_signal.copy() cut_f_signal[(w < band[0]) | (w > band[1])] = 0 cut_signal = irfft(cut_f_signal, axis=0) return cut_signal
def calculate_attributes(self): source = self.source freq = self.frequency sampling_rate = float(source.sampling_rate) fft_sampling_rate = sampling_rate/float(source.fft_step_size) window_length = float(source.fft_window_size)/sampling_rate # FIXME real time should be passed in as an extra field self.start = window_length if self.first_frame > 0: self.start += (self.first_frame - 1)/fft_sampling_rate self.length = window_length if len(freq) > 1: # if 'length' not in self.__dict__: # print self.__dict__ # print self.__dict__['length'] self.length += (len(freq) - 1)/fft_sampling_rate # print self.length self.end = self.start + self.length for k in ('frequency','amplitude'): a = getattr(self,k) setattr(self, k+'_min', min(a)) setattr(self, k+'_max', max(a)) setattr(self, k+'_mean', sum(a)/len(a)) freq_window = 2 # seconds freq_fft_size = 128 resampled_freq = resample(freq, freq_window*freq_fft_size/fft_sampling_rate, 'sinc_fastest') # FIXME truncate array? self.freq_fft = abs(rfft(resampled_freq,n=freq_fft_size,overwrite_x=True))[1:]
def test_size_accuracy(self): # Sanity check for the accuracy for prime and non-prime sized inputs if self.rdt == np.float32: rtol = 1e-5 elif self.rdt == np.float64: rtol = 1e-10 for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: np.random.seed(1234) x = np.random.rand(size).astype(self.rdt) y = irfft(rfft(x)) self.failUnless(np.linalg.norm(x - y) < rtol*np.linalg.norm(x), (size, self.rdt)) y = rfft(irfft(x)) self.failUnless(np.linalg.norm(x - y) < rtol*np.linalg.norm(x), (size, self.rdt))
def select_events(nevents,nfeatures): global groups fftbins = 8192 featurewidth = 16 print "Selecting %d random spectral features.." % nfeatures feature_bins = np.random.randint(featurewidth/2,(fftbins/8),nfeatures) print "Selecting %d random audio events.." % nevents events = np.random.randint(0,len(faudio)-grain_mid,nevents) # Initialise features array with the first variable as index features = np.zeros((nfeatures+1,nevents)) features[0] = np.arange(0,nevents) print "Computing audio event spectrograms.." # For each event.. for i in range(0,nevents): # Calculate spectrogram for the event _fftevent = faudio[events[i]:min(events[i]+grain_mid,len(faudio))]*sig.hann(grain_mid) mags = abs(rfft(_fftevent,fftbins)) mags = 20*log10(mags) # dB mags -= max(mags) # normalise to 0dB max # Calculate each feature for this event for j in range(0,nfeatures): features[j+1][i] = abs(np.mean(abs(mags[(feature_bins[j]-featurewidth/2):(feature_bins[j]+featurewidth/2)]))) print "Clustering events with K-Means algorithm.." groups = kmeans(np.transpose(features[1:,:]),tracks,minit='points',iter=30)[1] return [events,groups]
def show_magnitued(file_name): # read audio samples input_data = read(file_name) audio = input_data[1] print(audio) # apply a Hanning window window = hann(1024) audio = audio[0:1024] * window # fft mags = abs(rfft(audio)) # convert to dB mags = 20 * scipy.log10(mags) # normalise to 0 dB max # mags -= max(mags) file = open('tmp.txt', 'w') for i in mags: file.write(str(i) + '\n') file.close() # plot plt.plot(mags) # label the axes plt.ylabel("Magnitude (dB)") plt.xlabel("Frequency Bin") # set the title plt.title(file_name + " Spectrum") plt.show()
def get_power2(x, fs, band, n_sec=5): n_steps = int(n_sec * fs) w = fftpack.fftfreq(n_steps, d=1. / fs * 2) print(len(range(0, x.shape[0] - n_steps, n_steps))) pows = [2*np.sum(fftpack.rfft(x[k:k+n_steps])[(w > band[0]) & (w < band[1])]**2)/n_steps for k in range(0, x.shape[0] - n_steps, n_steps)] return np.array(pows)
def fft(self): t_plot = np.arange(0,self.T,self.dt) print len(t_plot) amplitude_record = [] for i in range(int(self.T/self.dt)): amplitude_record.append(self.string[i][20]) print len(amplitude_record) ''' plt.subplot(121) plt.title('String signal versus time') plt.ylabel('Signal (arbitrary units)') plt.xlabel('Time (s)') plt.plot(t_plot,amplitude_record) ''' freq = fftfreq(len(amplitude_record), d=self.dt) freq = np.array(abs(freq)) f_signal = rfft(amplitude_record) f_signal = np.array(f_signal**2) #plt.subplot(122) plt.title('Power spectra') plt.ylabel('Power (arbitrary units)') plt.xlabel('Frequency (Hz)') plt.xlim(2000,8000) plt.plot(freq,f_signal,label = 'epsilon = '+str(self.e)) return 0
def fft_filter(x, fs, band=(9, 14)): w = fftfreq(x.shape[0], d=1. / fs * 2) f_signal = rfft(x) cut_f_signal = f_signal.copy() cut_f_signal[(w < band[0]) | (w > band[1])] = 0 cut_signal = irfft(cut_f_signal) return cut_signal
def do_gen_random(peakAmpl, durationInMSec, samplingRate, fHigh, stereo=True): samples = durationInMSec * samplingRate / 1000 result = np.zeros(samples * 2 if stereo else samples, dtype=np.int16) randomSignal = np.random.normal(scale = peakAmpl * 2 / 3, size=samples) fftData = fft.rfft(randomSignal) freqSamples = samples/2 iHigh = freqSamples * fHigh * 2 / samplingRate + 1 #print len(randomSignal), len(fftData), fLow, fHigh, iHigh if iHigh > freqSamples - 1: iHigh = freqSamples - 1 fftData[0] = 0 # DC for i in range(iHigh, freqSamples - 1): fftData[ 2 * i + 1 ] = 0 fftData[ 2 * i + 2 ] = 0 if (samples - 2 *freqSamples) != 0: fftData[samples - 1] = 0 filteredData = fft.irfft(fftData) #freq = np.linspace(0.0, samplingRate, num=len(fftData), endpoint=False) #plt.plot(freq, abs(fft.fft(filteredData))) #plt.plot(filteredData) #plt.show() if stereo: for i in range(len(filteredData)): result[2 * i] = filteredData[i] result[2 * i + 1] = filteredData[i] else: for i in range(len(filteredData)): result[i] = filteredData[i] return result
def getFFT(self): freqs = [] fdatas = np.abs(fftpack.rfft(self.data,axis=1))/self.data.shape[1]*2.0 for fdata in fdatas: freq = fftpack.rfftfreq(int(fdata.shape[0]),1.0/self.sample_rate) freqs.append(freq) return (np.array(freqs), fdatas)
def lowHighCutFreqSmoothing(x, lowCut=400, hiCut=600): rft = fftpack.rfft(x) rft[:lowCut] = 0 rft[hiCut:] = 0 #y_smooth = sc.ifft(rft, N) y = fftpack.irfft(rft) return y #,y_smooth
def fftresample(S, npoints, reflect=False, axis=0): """ Resample a signal using discrete fourier transform. The signal is transformed in the fourier domain and then padded or truncated to the correct sampling frequency. This should be equivalent to a sinc resampling. """ from scipy.fftpack import rfft, irfft from dlab.datautils import flipaxis # this may be considerably faster if we do the memory operations in C # reflect at the boundaries if reflect: S = nx.concatenate([flipaxis(S,axis), S, flipaxis(S,axis)], axis=axis) npoints *= 3 newshape = list(S.shape) newshape[axis] = int(npoints) Sf = rfft(S, axis=axis) Sr = (1. * npoints / S.shape[axis]) * irfft(Sf, npoints, axis=axis, overwrite_x=1) if reflect: return nx.split(Sr,3)[1] else: return Sr
def bandpass(x, sampling_rate, f_min, f_max, verbose=0): """ xf = bandpass(x, sampling_rate, f_min, f_max) Description -------------- Phasen-treue mit der rueckwaerts-vorwaerts methode! Function bandpass-filters a signal without roleoff. The cutoff frequencies, f_min and f_max, are sharp. Arguements -------------- x: input timeseries sampling_rate: equidistant sampling with sampling frequency sampling_rate f_min, f_max: filter constants for lower and higher frequency Returns -------------- xf: the filtered signal """ x, N = np.asarray(x, dtype=float), len(x) t = np.arange(N)/np.float(sampling_rate) xn = detrend_linear(x) del t yn = np.concatenate((xn[::-1], xn)) # backwards forwards array f = np.float(sampling_rate)*np.asarray(np.arange(2*N)/2, dtype=int)/float(2*N) s = rfft(yn)*(f>f_min)*(f<f_max) # filtering yf = irfft(s) # backtransformation xf = (yf[:N][::-1]+yf[N:])/2. # phase average return xf
def fft_filter(x, fs, band=(9, 14)): w = fftpack.rfftfreq(x.shape[0], d=1. / fs) f_signal = fftpack.rfft(x, axis=0) cut_f_signal = f_signal.copy() cut_f_signal[(w < band[0]) | (w > band[1])] = 0 cut_signal = fftpack.irfft(cut_f_signal, axis=0) return cut_signal
def rfft_freq(data, window_func=signal.hanning): w = window_func(data.size) sig_fft = fftpack.rfft(data * w) freq = fftpack.rfftfreq(sig_fft.size, d=SAMPLING_INTERVAL) freq = freq[range(data.size / 2)] sig_fft = sig_fft[range(data.size / 2)] return sig_fft, freq
def freq_from_fft(self, p, threshold=5000): """ Estimate frequency from peak of FFT """ # Debugging. # start_time = time.time() bits_per_sample = p.get_sample_size(self.FORMAT) * 8 dtype = 'int{0}'.format(bits_per_sample) sig = np.frombuffer(b''.join(self.frames), dtype) fs = self.RATE # Compute Fourier transform of windowed signal windowed = sig * blackmanharris(len(sig)) f = rfft(windowed) # Find the peak and interpolate to get a more accurate peak i = np.argmax(abs(f)) # Just use this for less-accurate, naive version true_i = self.parabolic(np.log(abs(f)), i)[0] # Convert to equivalent frequency if (fs * true_i / len(windowed)) > threshold: if self.scratching == False: print("Sent message!") arrow_down_event = pg.event.Event(pg.KEYDOWN, key=pg.K_RIGHT) pg.event.post(arrow_down_event) self.scratching = True self.scratch_started_time = time.time() # print(self.scratch_started_time) # Debugging print('Scratch detected!') print(fs * true_i / len(windowed))
def plot_gated_fft(x, x_p, genome, conf, t): x_pt = x_p.transpose(0, 1) net = RecurrentNet.create(genome, conf, device="cpu", dtype=torch.float32) net.reset() gate = [] # norm = torch.zeros(batch_size) for xi in x_pt: xo = net.activate(xi) # batch_size x 2 # score = xo[:, 1] confidence = xo[:, 0] gate.append(xo[:, 0].numpy()) # contribution += score * confidence # batch_size # norm += confidence # gate: t x batch_size(=1) gate = np.array(gate) assert gate.shape[1] == 1 gate.flatten() gate = gate.repeat(512) filtered_signal = gate[:len(x)] * x.numpy() # plt.figure() # plt.plot(x) # plt.plot(filtered_signal) # plt.plot(gate) # plt.show() print(t) x_fft = rfft(x.numpy()) plt.figure() plt.title("signal female" if t else "signal male") plt.xscale("log") plt.plot(x_fft) plt.show() x_fft = rfft(filtered_signal) plt.figure() plt.title("filtered signal female" if t else "filtered signal male") plt.xscale("log") plt.plot(x_fft[:len(x_fft)]) plt.show()
def FFT(signal, dT): """ :param signal: [array] :param dT: sample space [float] """ ampl = np.abs(rfft(signal)) * 2.0 / len(signal) freq = rfftfreq(len(ampl), d=dT) return ampl, freq
def fourierTransformResponses(standards, deviants, shortestOverallStandard, shortestOverallDeviant): fourierResponses = [] types = [] clip = (shortestOverallDeviant if shortestOverallDeviant < shortestOverallStandard else shortestOverallStandard) for electrode in range(0, 3): for response in standards[electrode]: amplitudes = rfft(response) fourierResponses.append(abs(amplitudes[0:clip]).tolist()) types.append('standard') for response in deviants[electrode]: amplitudes = rfft(response) fourierResponses.append(abs(amplitudes[0:clip]).tolist()) types.append('deviant') return fourierResponses, types
def get_feat(file_name): a, sr = librosa.load(file_name) fft_wave = fftpack.rfft(a, n=sr) fft_freq = fftpack.rfftfreq(n=sr, d=1/sr) y = librosa.amplitude_to_db(fft_wave, ref=np.max) # plt.plot(fft_freq, y) # plt.show() return y
def test_definition(self): for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]: x = np.array(t, dtype=self.rdt) y = rfft(x) y1 = direct_rdft(x) assert_array_almost_equal(y,y1) self.assertTrue(y.dtype == self.rdt, "Output dtype is %s, expected %s" % (y.dtype, self.rdt))
def getFundFreqs(audio): # Calculate Mean Frequency freq_data = sfp.rfft(audio) freq_data = np.abs(freq_data) freq_data = freq_data[freq_data <= 280.0] freq_data = freq_data[freq_data != 0.0] freq_mean = np.abs(np.mean(freq_data)) return freq_mean
def low_filter(data, sample_rate=1000): f_signal = rfft(data) l = int(len(f_signal) * 5.0 / sample_rate) cut_f_signal = f_signal.copy() cut_f_signal[0:l + 1] = 0 cut_f_signal[len(f_signal) + 1 - l:] = 0 cut_signal = irfft(cut_f_signal) return cut_signal
def get_windowed_fft(data, block_length): # how many blocks have to be processed? num_blocks = int(np.ceil(len(data) / block_length)) w_fft = [] if num_blocks == 1: w_fft = fft.rfft(data) else: for i in range(0, num_blocks): start = i * block_length stop = np.min([(start + block_length - 1), len(data)]) f = fft.rfft(data[start:stop]) w_fft.append(f.var()) return np.asarray(w_fft)
def _low_pass(self, wave, cutoff_freq=1.0): signal = fftpack.rfft(wave) freq = fftpack.fftfreq(len(wave)) * self.SF cut_signal = signal.copy() cut_signal[(np.abs(freq) > cutoff_freq)] = 0 return fftpack.irfft(cut_signal)
def get_params(y): A=(np.max(y)-np.min(y))/2 B=np.mean(y) freqs = np.fft.fftfreq(len(y)) index=np.where(rfft(y) == np.max(rfft(y)[1:]))[0] n=np.pi*freqs[index][0] rest0=1e8 fi0=0 for fi in range(0,60,1): fi=fi/10 rest=np.abs(y[0]-(A*np.sin(fi*n)+B)) if rest<rest0: rest0=rest fi0=fi popt=[A,fi0,n,B] return popt
def rfft_y_base(array, sample_rate): #change file format array = np.array(array) yt = np.ravel(array) #rfft yf = np.abs(rfft(yt, n=sample_rate, axis=0)) xf = rfftfreq(yf.size, d=1. / sample_rate).reshape(-1, 1) return (yf, xf)
def FFT_brickwallHPF(filename, cutoff, wout=True, plot=True): """ Deletes frequencies below cutoff using brickwall. Beware of phase issues. Parameters ---------- filename: string Name of the input <audio> file. Uses the soundfile python library to decode the input. cutoff: int (Hz) Frequency of the filter cutoff below which data is filtered out. wout: True/False, optional, default=True Writes the data to a 16 bit *.wav file. Equating to false will suppress *.wav output, for example if you want to chain process. plot: True/False, optional, default=True Produces plot of raw wave form and processed waveform. Returns ------- data_filtered: array containing filtered audio in bits """ n, data, data_dB, sr, ch = inputwav(filename) print('Applying FFT...') yfreq = rfft(data, axis=0) xfreq = np.linspace(0, sr / (2.0), n) yfreqBHPF = np.zeros((n, ch)) yfreqBHPF[0:n, :] = yfreq print('Applying brickwall at ' + str(cutoff) + ' Hz...') yfreqBHPF[0:np.searchsorted(xfreq, cutoff), :] = 0.0 data_filtered = (irfft(yfreqBHPF, axis=0)) if wout == True: print('Exporting...') sf.write(filename[0:len(filename) - 4] + '_brickwallHPF.wav', data_filtered, sr, 'PCM_16') if plot == True: print('Plotting...') py.close() fig, (ax1, ax2) = py.subplots(nrows=2) ax1.semilogx(xfreq, 20 * np.log10(abs(yfreq[0:n, 0] + .0001)), 'k-', lw=0.5) ax1.semilogx(xfreq, 20 * np.log10(abs(yfreqBHPF[0:n // 1, 0] + .0001)), 'm-', lw=0.1) ax1.set_xlabel('Frequency (Hz)') ax1.set_ylabel('Amplitude') ax2.plot(data, 'k-', label='Raw') ax2.plot(data_filtered, 'm-', label='Filtered') ax2.set_xlim(0, 10000) ax2.set_ylim(-1, 1) ax2.set_ylabel('Amplitude (Norm Bits)') ax2.set_xlabel('Samples') ax2.legend(loc=2) print('Done!') return data_filtered
def detect_events(sample_freq, queue): """ Does FFT based event detection and plotting. :type sample_freq: int :param sample_freq: Sampling frequency in Hz :type queue: Multiprocessing Queue :param queue: detect_events() reads from this queue to acquire batches of IMU data captured by the acquire_data() process. :return: None """ # detect_events() blocks on the queue read, so data processing rate is driven by the rate that # ac while True: imu_data_dict = queue.get() # Check the timestamp on this data frame. If it's more than one second behind the current # CLOCK_MONOTONIC_RAW time, then detect_events() isn't keeping up with the incoming stream # of IMU measurements comming from acquire_data(). Jump back to the top of the while loop, # and get another data frame. We'll keep doing that until we're reading fresh data again. # TODO: the threshold here might need to be adjusted, and perhaps the whole queue should be cleared. if (time.clock_gettime(time.CLOCK_MONOTONIC_RAW) - imu_data_dict['timestamp']) > 1: continue df = pd.DataFrame(data=imu_data_dict['w_vel'][0], columns=['x']) # print("Number of NaN points: " + str(df.isna().sum())) # Doing a linear interpolation that replaces the NaN placeholders for desynced, dropped data # with a straight line between the last good data point before a dropout and the next good data # point. This does seem to improve the signal-to-noise ratio in the FFT. df = df.interpolate() yf = fftpack.rfft(df.loc[:, 'x']) # Is this a better way of getting the power spectrum than numpy.abs(yf?) yf = numpy.sqrt((yf * yf.conj()).real) xf = fftpack.rfftfreq((len(df.index)), 1./sample_freq) peaks, peaks_dict = signal.find_peaks(yf, height=1) #print(xf[peaks], yf[peaks]) #print(peaks, peaks_dict) # Check if any peak has been detected between 3 and 5 Hz, along the x axis. This is our rule for detecting # tooth-brushing behavior. #if numpy.where(numpy.logical_and(xf[peaks] >= 3, xf[peaks] <= 5))[0].size > 0: if numpy.where(peaks_dict['peak_heights'] > 800)[0].size > 0: print("Scratching Detected!")
def plotFFT(self): global fileselected, oldt, oldavg1, oldavg2, oldavg3 if not fileselected: #### Plot FFT of data from red rectangle #### t_length = len(oldt) dt = (max(oldt) - min(oldt)) / (t_length - 1) red_no_dc = oldavg1 - np.mean(oldavg1) yf1 = rfft(red_no_dc) tf = np.linspace(0.0, 1.0 / (2.0 * dt), t_length // 2) i = np.argmax(abs(yf1[0:t_length // 2])) redpeak = tf[i] peakfind1 = str('Red peak at ' + str(round(redpeak, 2)) + ' Hz or ' + str(round(1 / redpeak, 2)) + ' s') print(peakfind1) pen1 = pg.mkPen('r', width=1, style=QtCore.Qt.SolidLine) self.plotFFTred.plot(tf, np.abs(yf1[0:t_length // 2]), pen=pen1, clear=True) #### Plot FFT of data from green rectangle #### green_no_dc = oldavg2 - np.mean(oldavg2) yf2 = rfft(green_no_dc) j = np.argmax(abs(yf2[0:t_length // 2])) greenpeak = tf[j] peakfind2 = str('Green peak at ' + str(round(greenpeak, 2)) + ' Hz or ' + str(round(1 / greenpeak, 2)) + ' s') print(peakfind2) pen2 = pg.mkPen('g', width=1, style=QtCore.Qt.SolidLine) self.plotFFTgreen.plot(tf, np.abs(yf2[0:t_length // 2]), pen=pen2, clear=True) #### Plot FFT of data from blue rectangle #### blue_no_dc = oldavg3 - np.mean(oldavg3) yf3 = rfft(blue_no_dc) k = np.argmax(abs(yf3[0:t_length // 2])) bluepeak = tf[k] peakfind3 = str('Blue peak at ' + str(round(bluepeak, 2)) + ' Hz or ' + str(round(1 / bluepeak, 2)) + ' s') print(peakfind3) pen3 = pg.mkPen('b', width=1, style=QtCore.Qt.SolidLine) self.plotFFTblue.plot(tf, np.abs(yf3[0:t_length // 2]), pen=pen3, clear=True)
def compute_interpeak(data, sample_rate): freqs = fftfreq(data.size, d=1.0/sample_rate) f_signal = rfft(data) imax_freq = np.argsort(f_signal)[-2] freq = np.abs(freqs[imax_freq]) # tepe noktalari arasindaki veri nokta sayisi interpeak = np.int(np.round(sample_rate / freq)) return interpeak
def convert_freq(self): self.original_complex = rfft(self.original_sig) self.modified_complex = np.copy(self.original_complex) self.freq = (rfftfreq( len(self.original_complex) + 1, 1 / self.sample_rate)) self.freq = self.freq[self.freq > 0] print(self.freq[len(self.freq) - 1]) self.data_line.setData(self.freq, abs(self.modified_complex)) self.ui.modified_frequ.setXRange(0, 20000)
def FFT_deNoise(y, dx, noise_level, noise_filter=0.1): w = rfft(y) f = rfftfreq(len(y), dx) spectrum = w**2 cutoff = spectrum < (spectrum.max()*noise_level*noise_filter) w_clean = w.copy() w_clean[cutoff] = 0 y_clean = irfft(w_clean) return f, spectrum, w_clean, y_clean
def get_power2(x, fs, band, n_sec=5): n_steps = int(n_sec * fs) w = fftfreq(n_steps, d=1. / fs * 2) print(len(range(0, x.shape[0] - n_steps, n_steps))) pows = [ 2 * np.sum(rfft(x[k:k + n_steps])[(w > band[0]) & (w < band[1])]**2) / n_steps for k in range(0, x.shape[0] - n_steps, n_steps) ] return np.array(pows)
def doFFT(self): # from https://stackoverflow.com/questions/23377665/python-scipy-fft-wav-files from scipy.fftpack import rfft self.fftData = rfft(self.normalizedData) # calculate real FFT self.setFFTnumUsefulBins() self.setFFTbinSize() self.calculateFFTABS()
def smooth_line(y, fd): # fd - частота дискретизации W = fftfreq(y.size, 1 / fd) f_signal = rfft(y) cut_f_signal = f_signal.copy() cut_f_signal[(W < 0.25)] = 0 cut_f_signal[(W > 60)] = 0 cut_signal = irfft(cut_f_signal) return cut_signal
def fft_data(self): num = len(self.b) noct = int(log(num) / log(2)) if (noct > 20): noct = 20 num_fft = 2**noct bb = self.b[0:num_fft] if (self.imr == 1): bb = bb - mean(bb) dur_fft = self.a[num_fft - 1] - self.a[0] df = 1 / dur_fft z = fft(bb) k = numpy.fft.fftfreq(len(bb))[range(0, num_fft)] freq_pwr = 10 * log10(1e-20 + abs(rfft(bb, num_fft))) #fo = open("power.txt","a") #fo.write(str(freq_pwr)) #fo.write(' ') #fo.close() nhalf = num_fft / 2 zz = zeros(nhalf, 'f') ff = zeros(nhalf, 'f') ph = zeros(nhalf, 'f') freq = zeros(num_fft, 'f') z /= float(num_fft) h = int(num_fft) for k in range(0, int(num_fft)): freq[k] = k * df ff = freq[0:nhalf] for k in range(0, int(nhalf)): if (k > 0): zz[k] = 2. * abs(z[k]) else: zz[k] = abs(z[k]) ph[k] = atan2(z.real[k], z.imag[k]) idx = argmax(abs(zz)) return idx, freq, ff, z, zz, ph, nhalf, df, num_fft
def create_non_tda_features(path, fourier_window_size=[], rolling_mean_size=[], rolling_max_size=[], rolling_min_size=[], mad_size=[], fourier_coefficients=[]): """ INPUT: path: int (number to OpenML dataset) fourier_window_size: a list of window sizes. Note: min must be > max(fourier_coefficients) rolling_mean_size: a list of window sizes rolling_max_shift: a list of window sizes rolling_min_shift: a list of window sizes mad_size: a list of window sizes fourier_coefficients: a list of all fourier coefficients to include. Note: max must be < min(fourier_window_size) OUTPUT: df: pandas dataframe with columns: max_... for rolling max features min_... for rolling min features mean_... for rolling mean features mad_... for rolling mad features fourier_... for fourier coefficients """ df = get_dataset(path) df = df.get_data()[0] df.rename({ 'label': 'y', 'coord_0': 'x', 'coord_1': 'x_dot' }, axis='columns', inplace=True) pandarallel.initialize() for r in rolling_max_size: df['max_' + str(r)] = df['x'].rolling(r).max() for r in rolling_mean_size: df['mean_' + str(r)] = df['x'].rolling(r).mean() for r in rolling_min_size: df['min_' + str(r)] = df['x'].rolling(r).min() for r in mad_size: df['mad_' + str(r)] = df['x'] - df['x'].rolling(r).min() if (not fourier_coefficients and fourier_window_size) or (not fourier_window_size and fourier_coefficients): print('Need to specify the fourier coeffcients and the window size') for n in fourier_coefficients: df[f'fourier_w_{n}'] = df['x'].rolling( fourier_window_size).parallel_apply(lambda x: rfft(x)[n], raw=False) # Remove all rows with NaNs df.dropna(axis='rows', inplace=True) return df
def applyParameter(self, data): #low pass filter fftdata = rfft(data) fftdata[int(float(self._variables["FREQUENCY"])*np.pi*2):] = 0 ##removes negative values filtered_data = irfft(fftdata).clip(0) return filtered_data
def test_non_ndarray_with_dtype(self): x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) xs = _TestRFFTBase.MockSeries(x) expected = [1, 2, 3, 4, 5] out = rfft(xs) # Data should not have been overwritten assert_equal(x, expected) assert_equal(xs.data, expected)
def rfft(self, X): """ Apply real FFT to X Parameters ---------- X : list of lists or ndArrays """ X = np.array([rfft(x) for x in X]) return X
def get_spectrogram(x, width, skip): lines = [] for i in xrange((len(x) - width)/skip): #print i, "/", (len(x) - width)/skip ft = fftp.rfft(x[i*skip:i*skip+width])[1:] lines.append(abs(ft)) #lines.append(ft**2) #lines.append(np.array(map(complex_to_rgb, ft))) res = np.array(lines) res = np.swapaxes(res, 0, 1) return res
def bandpass(data_list, min_hz, max_hz): fft_list = rfft(data_list) # Filter for i in range(len(fft_list)): if not (min_hz < i/2+1 < max_hz): fft_list[i] = 0 result_vals = irfft(fft_list) return result_vals