示例#1
0
def window(f,start,stop,type='blackman'):
    """
    runs the data through a hamming window.
    @param f: The data matrix
    @param start: The start index of the hamming window.
    @param stop: The end index of the hamming window.
    """
    h=numpy.zeros(f.shape,dtype=float)

    if len(h.shape)==1:
        if type=='hamming':
            h[start:stop]=signal.hamming(stop-start)
        elif type=='blackman':
            h[start:stop]=signal.blackman(stop-start)
        elif type=='hann':
            h[start:stop]=signal.hann(stop-start)
        elif type=='blackmanharris':
            h[start:stop]=signal.blackmanharris(stop-start)
        elif type=='rectangular' or type=='rect' or type=='boxcar':
            h[start:stop]=signal.boxcar(stop-start)
    else:
        if type=='hamming':
            h[:,start:stop]=signal.hamming(stop-start)
        elif type=='blackman':
            h[:,start:stop]=signal.blackman(stop-start)
        elif type=='hann':
            h[:,start:stop]=signal.hann(stop-start)
        elif type=='blackmanharris':
            h[:,start:stop]=signal.blackmanharris(stop-start)
        elif type=='rectangular' or type=='rect' or type=='boxcar':
            h[:,start:stop]=signal.boxcar(stop-start)
    return numpy.multiply(f,h)
示例#2
0
 def test_basic(self):
     assert_allclose(signal.hamming(6, False),
                     [0.08, 0.31, 0.77, 1.0, 0.77, 0.31])
     assert_allclose(signal.hamming(6),
                     [0.08, 0.3978521825875242, 0.9121478174124757,
                      0.9121478174124757, 0.3978521825875242, 0.08])
     assert_allclose(signal.hamming(7, sym=True),
                     [0.08, 0.31, 0.77, 1.0, 0.77, 0.31, 0.08])
示例#3
0
    def __init__(self,freq,N_samples):

        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.createQtConnections()


        self.sample_rate = 2.4e5 ###1e6
        #self.decim_r1 = 1e6/2e5 # for wideband fm
        self.decim_r2 = 2.4e5/48000 # for baseband recovery
        self.center_freq = freq #+250e3
        self.gain = 38

        self.N_samples = N_samples
        self.is_sampling = False

        self.spectrogram = np.zeros((328,200))
        self.chspectrogram = np.zeros((328,200))
        self.plspectrogram = np.zeros((164,200))

        self.sdr =  RtlSdr()
        #self.sdr.direct_sampling = 1
        self.sdr.sample_rate = self.sample_rate
        self.sdr.center_freq = self.center_freq
        self.sdr.gain = self.gain

        self.pa = pyaudio.PyAudio()
        self.stream = self.pa.open( format = pyaudio.paFloat32,
                                    channels = 2,
                                    rate = 48000,
                                    output = True)

        adj = 0
        hamming = np.kaiser(self.N_samples/4 + adj,1)
        lpf = np.append( np.zeros(self.N_samples*3/8),hamming)
        self.lpf = np.fft.fftshift(np.append(lpf,np.zeros(self.N_samples*3/8))) #,int(-.25*self.N_samples))

        hamming = 10*signal.hamming(self.N_samples/16)
        lpf = np.append(np.zeros(self.N_samples*15/32),hamming)
        self.lpf_s1 = (np.append(lpf,np.zeros(int(self.N_samples*15/32))))
        #self.lpf_s1 = np.roll(temp,int(.5*self.N_samples*67/120))
        #self.lpf_s1 += np.roll(temp,int(-.5*self.N_samples*67/120))
        self.lpf_s1 = np.fft.fftshift(self.lpf_s1)
        #self.lpf_s1 += np.fft.fftshift(self.lpf_s1)

#         fig = plt.figure()
#         ax = fig.add_subplot(111)
#         ax.plot(range(self.lpf_s1.size),self.lpf_s1)
#         fig.show()

        hamming = 10*signal.hamming(self.N_samples/32)
        lpf = np.append(np.zeros(self.N_samples*31/64),hamming)
        self.lpf_s2 = (np.append(lpf,np.zeros(int(self.N_samples*31/64))))
        #self.lpf_s2 = np.roll(temp,int(.5*self.N_samples*92/120))
        #self.lpf_s2 += np.roll(temp,int(-.5*self.N_samples*92/120))
        self.lpf_s2 = np.fft.fftshift(self.lpf_s2)
def read_data_thread(rf_q,ad_rdy_ev,audio_q):
    print("read data thread start")
    pre_data=0
    #filter design
    #FIR_LP = signal.firwin(19,17e3,1e3,window='hamming',True,False,RFRATE/2)
    #FIR_LP = signal.firwin(19,400e3/(RFRATE/2))
    #FIR_LP = signal.firwin(9,10e3/(AUDIORATE/2))
    rfwindow = signal.hamming(RFSIZE)
    audiowindow = signal.hamming(AUDIOSIZE)
    fftwindow = signal.hamming(512)

    ad_rdy_ev.wait(timeout=5000)
    np.save("data",rf_q.get())
    while 0:
        ad_rdy_ev.wait(timeout=1000)
        while not rf_q.empty():
            #process data here
            data=rf_q.get()
            #data=signal.decimate(data,DOWN_FACTOR,ftype="fir")

            #data = signal.lfilter(FIR_LP,1.0,data)

            #demod method 1
            angle_data=np.angle(data)
            audioda=np.diff(angle_data)
            audiodata=np.insert(audioda,0,angle_data[0]-pre_data)
            pre_data=angle_data[-1]
            audiodata=np.unwrap(audiodata,np.pi)


            #demod method 2
            #data_delay=np.insert(data,0,pre_data)
            #pre_data = data_delay[-1]
            #data_delay=np.delete(data_delay,-1)
            #diff_data=data*np.conj(data_delay)
            #audiodata=np.angle(diff_data)
            #audiodata=np.unwrap(audiodata)


            #demod method 3
            #diff_data=np.diff(data)
            #diff_data=np.insert(diff_data,0,data[0]-pre_data)
            #pre_data=data[-1]
            #audiodata=data.real*diff_data.imag-data.imag*diff_data.real
            #audiodata=audiodata/(np.power(data.real,2)+np.power(data.imag,2))
            #audiodata=audiodata*10

            audiodata=signal.decimate(audiodata,DOWN_FACTOR,ftype="fir")

            #audiodata = signal.lfilter(FIR_LP,1.0,audiodata)

            audiodata_amp=audiodata*1e4
            snd_data = audiodata_amp.astype(np.dtype('<i2')).tostring()
            audio_q.put(snd_data)
        ad_rdy_ev.clear()
    print("read data thread ended")
示例#5
0
 def test_basic(self):
     assert_allclose(signal.hamming(6, False),
                     [0.08, 0.31, 0.77, 1.0, 0.77, 0.31])
     assert_allclose(signal.hamming(7, sym=False),
                     [0.08, 0.2531946911449826, 0.6423596296199047,
                      0.9544456792351128, 0.9544456792351128,
                      0.6423596296199047, 0.2531946911449826])
     assert_allclose(signal.hamming(6),
                     [0.08, 0.3978521825875242, 0.9121478174124757,
                      0.9121478174124757, 0.3978521825875242, 0.08])
     assert_allclose(signal.hamming(7, sym=True),
                     [0.08, 0.31, 0.77, 1.0, 0.77, 0.31, 0.08])
    def enframe(self, datas, fs, frame_len, frame_inc, win):
        '''
        ' datas: 语音数据
        ' fs: 采样频率
        ' frame_len: 帧长,单位秒
        ' frame_inc: 帧移,单位秒
        ' win: 窗函数
        '''
        datas_len = len(datas)   # 数据总长度
        frame_len = int(round(frame_len * fs))   # 帧长,数据个数
        nstep = frame_len - int(round(frame_inc * fs))   # 帧移动步长,数据个数

        if datas_len < frame_len: # 若信号长度小于帧长,则帧数定义为1
            nf = 1
        else: 
            nf = int(np.ceil((1.0*datas_len-frame_len)/nstep)) + 1

        pad_len = int((nf-1)*nstep + frame_len)    # 所有帧总数据长度
        # 多余的数据使用0填充
        new_datas = np.concatenate((datas, np.zeros(pad_len - datas_len)))

        indices = np.tile(np.arange(0,frame_len),(nf,1))+np.tile(np.arange(0,nf*nstep,nstep),(frame_len,1)).T  
        indices = np.array(indices, dtype = np.int32) # 否则会报类型错误

        frames = new_datas[indices] #得到帧信号

        # 加窗
        if win == 'hamming':
            win = signal.hamming(frame_len) 
        elif win == 'hanning':
            win = signal.hanning(frame_len)
        else:
            win = signal.boxcar(frame_len)

        return frames * np.tile(win, (nf, 1))
示例#7
0
    def __init__(self,freq,N_samples):

        self.sample_rate = 1e6
        self.decim_r1 = 1e6/2e5 # for wideband fm
        self.decim_r2 = 2e5/44100 # for baseband recovery
        self.center_freq = freq+250e3
        self.gain = 36

        self.N_samples = N_samples

        self.sdr =  RtlSdr()
        self.sdr.direct_sampling = 1
        self.sdr.sample_rate = self.sample_rate
        self.sdr.center_freq = self.center_freq
        self.sdr.gain = 'auto' #self.gain

        self.pa = pyaudio.PyAudio()
        self.stream = self.pa.open( format = pyaudio.paFloat32,
                                    channels = 2,
                                    rate = 44100,
                                    output = True)

        adj = 0
        hamming = 10*signal.hamming(self.N_samples*.10 + adj)
        lpf = np.append( np.zeros(self.N_samples*.45),hamming)
        self.lpf = np.roll(np.fft.fftshift(np.append(lpf,np.zeros(self.N_samples*.45))),int(-.25*self.N_samples))
示例#8
0
def mfcc(samples, winlen, winshift, nfft, nceps, samplingrate):
    """Computes Mel Frequency Cepstrum Coefficients.
    Args:
        samples: array of speech samples with shape (N,)
        winlen: lenght of the analysis window
        winshift: number of samples to shift the analysis window at every time step
        nfft: length of the Fast Fourier Transform (power of 2, grater than winlen)
        nceps: number of cepstrum coefficients to compute
        samplingrate: sampling rate of the original signal
    Note: for convenienve, you can define defaults for the input arguments that fit the exercise
    Returns:
        ceps: N x nceps array with one MFCC feature vector per row
        mspec: N x M array of outputs of the Mel filterbank (of size M)
        spec: N x nfft array with squared absolute fast Fourier transform
    """

    enframes = enframe(samples, winlen, winshift)
    # preemp_signal = map(lambda x: preemp(x, 0.97), enframes)
    preemp_signal = preemp(enframes, p=0.97)
    hamWindow = hamming(winlen, False)
    ham_signal = helper.combineHam(preemp_signal, hamWindow)

    if not nfft:
        nfft = 512

    spec, logspec_fft = fft(ham_signal, nfft);

    bank1 = tools.trfbank(samplingrate, nfft);
    mspec = helper.melSpec(spec, bank1)
    spec_dct = helper.cosineTransform(mspec)
    ceps = spec_dct[:, :nceps]

    return (spec, mspec, ceps)
示例#9
0
def CQT_fast(x,fs,bins,fmin,fmax,M):

	threshold = 0.0054 #for Hamming window
	K = int(bins*np.ceil(np.log2(fmax/fmin)))
	Q = 1/(2**(1/bins)-1)
	nfft = np.int32(nearestPow2(np.ceil(Q*fs/fmin)))
	tempKernel = np.zeros(nfft, dtype = np.complex)
	specKernel = np.zeros(nfft, dtype = np.complex)
	sparKernel = []

	#create sparse Kernel 
	for k in range(K-1,-1,-1):
		fk = (2**(k/bins))*fmin
		N = np.int32(np.round((Q*fs)/fk))
		tempKernel[:N] = hamming(N)/N * np.exp(-2*np.pi*1j*Q*np.arange(N)/N)
		specKernel = fft(tempKernel)
		specKernel[np.where(np.abs(specKernel) <= threshold)] = 0
		if k == K-1:
			sparKernel = specKernel
		else:
			sparKernel = np.vstack((specKernel, sparKernel))
	
	sparKernel = np.transpose(np.conjugate(sparKernel))/nfft
	ft = fft(x,nfft)
	cqt = np.dot(ft, sparKernel)
	ft = fft(x,nfft*(2**M))
	#calculate harmonic power spectrum
	#harm_pow = HPS(ft,M)
	#cqt = np.dot(harm_pow, sparKernel)
	return cqt
示例#10
0
 def setUp(self):
     path = join(dirname(__file__), "data")
     # setting up sliding window data
     data_z = np.loadtxt(join(path, "MBGA_Z.ASC"))
     data_e = np.loadtxt(join(path, "MBGA_E.ASC"))
     data_n = np.loadtxt(join(path, "MBGA_N.ASC"))
     n = 256
     fs = 75
     inc = int(0.05 * fs)
     self.data_win_z, self.nwin, self.no_win = util.enframe(data_z, signal.hamming(n), inc)
     self.data_win_e, self.nwin, self.no_win = util.enframe(data_e, signal.hamming(n), inc)
     self.data_win_n, self.nwin, self.no_win = util.enframe(data_n, signal.hamming(n), inc)
     # global test input
     self.fk = [2, 1, 0, -1, -2]
     self.norm = pow(np.max(data_z), 2)
     self.res = np.loadtxt(join(path, "3cssan.hy.1.MBGA_Z"))
示例#11
0
def mteo (x, k_values):
    """
    Multi resolution Teagre Energy Operator

    Parameters
    ----------
    x : ndarray
        The raw signal

    k_values : ndarray
        The values of k to use for estimating MTEO

    Returns
    -------
    tem : ndarray
        The operated signal
    """
    from scipy.signal import hamming
    from scipy.signal import lfilter

    teo_k_wise = np.zeros((len(k_values), len(x)))
    for i in range(len(k_values)):
        teo_k_wise[i, :] = teo(x, k_values[i])
        variance = np.var(teo_k_wise[i, :])
        window = hamming(4*k_values[i] + 1)
        teo_k_wise[i, :] = lfilter(window, 1, teo_k_wise[i, :]) / variance
    
    tem = np.max(teo_k_wise, axis=0)
    return tem
示例#12
0
def unsharp_masking(X):
    lp = np.array(X)
    for i, ws in zip([0, 1, 2], [50, 50, 25]):
        h = hamming(ws)
        h /= h.sum()
        convolve1d(lp, h, axis=i, output=lp)
    return X - lp
示例#13
0
文件: icsd.py 项目: Junji110/iCSD
 def filter_csd(self):
     '''Spatial filtering of the CSD estimate, using an N-point filter'''
     if not self.f_order > 0 and type(self.f_order) == type(3):
         raise Exception, 'Filter order must be int > 0!'
     
     if self.f_type == 'boxcar':
         num = ss.boxcar(self.f_order)
         denom = pl.array([num.sum()])
     elif self.f_type == 'hamming':
         num = ss.hamming(self.f_order)
         denom = pl.array([num.sum()])
     elif self.f_type == 'triangular':
         num = ss.triang(self.f_order)
         denom = pl.array([num.sum()])
     elif self.f_type == 'gaussian':
         num = ss.gaussian(self.f_order[0], self.f_order[1])
         denom = pl.array([num.sum()])
     else:
         raise Exception, '%s Wrong filter type!' % self.f_type
     
     num_string = '[ '
     for i in num:
         num_string = num_string + '%.3f ' % i
     num_string = num_string + ']'
     denom_string = '[ '
     for i in denom:
         denom_string = denom_string + '%.3f ' % i
     denom_string = denom_string + ']'
     
     print 'discrete filter coefficients: \nb = %s, \na = %s' % \
                                                  (num_string, denom_string) 
     self.csd_filtered = pl.empty(self.csd.shape)
     for i in xrange(self.csd.shape[1]):
         self.csd_filtered[:, i] = ss.filtfilt(num, denom, self.csd[:, i])
示例#14
0
def data_w_hamm(dt,frame=256):
    temp = []
    _t = sig.hamming(frame)
    fx = frame*0.5
    #temp = [sum(np.array(dt[x*fx:x*fx+frame]*_t)**2) for x in range(int(len(dt)/fx -1))]
    temp = [np.log(sum(np.abs(dt[x*fx:x*fx+frame]*_t))) for x in range(int(len(dt)/fx -1))]
    return temp
    def __init__(self, clip, window=1024, step=None, n=None):
        """Compute the short-time Fourier transform on a 1-dimensional array
        *signal*, with the specified *window* size, *step* size, and
        *n*-resolution FFT.

        This function returns a 2-dimensional array of complex floats. The
        0th dimension is time (window steps) and the 1th dimension is
        frequency.
        """
        if clip is None:
            return
        
        if step is None:
            step = window / 2
        if n is None:
            n = window

        signal = clip.signal
        self.params = (window, step, n, clip.nyq)

        if signal.ndim != 1:
            raise ValueError("signal must be a 1-dimensional array")
        length = signal.size
        num_windows = _num_windows(length, window, step)
        out = np.zeros((num_windows, n), dtype=np.complex64)
        taper = hamming(window)
        for (i, s) in enumerate(window_slice_iterator(length, window, step)):
            out[i, :] = np.fft.fft(signal[s] * taper, n)
        self.data = out
示例#16
0
def plot_specgram(ax, data, fs, nfft=256, noverlap=128, window='hann',
                  cmap='jet', interpolation='bilinear', rasterized=True):

    if window not in SPECGRAM_WINDOWS:
        raise ValueError("Window not supported")

    elif window == "boxcar":
        mwindow = signal.boxcar(nfft)
    elif window == "hamming":
        mwindow = signal.hamming(nfft)
    elif window == "hann":
        mwindow = signal.hann(nfft)
    elif window == "bartlett":
        mwindow = signal.bartlett(nfft)
    elif window == "blackman":
        mwindow = signal.blackman(nfft)
    elif window == "blackmanharris":
        mwindow = signal.blackmanharris(nfft)

    specgram, freqs, time = mlab.specgram(data, NFFT=nfft, Fs=fs,
                                          window=mwindow,
                                          noverlap=noverlap)
    specgram = 10 * np.log10(specgram[1:, :])
    specgram = np.flipud(specgram)

    freqs = freqs[1:]
    halfbin_time = (time[1] - time[0]) / 2.0
    halfbin_freq = (freqs[1] - freqs[0]) / 2.0
    extent = (time[0] - halfbin_time, time[-1] + halfbin_time,
              freqs[0] - halfbin_freq, freqs[-1] + halfbin_freq)

    ax.imshow(specgram, cmap=cmap, interpolation=interpolation,
                            extent=extent, rasterized=rasterized)
    ax.axis('tight')
示例#17
0
def STIFT(stft_windows, nfft, nskip):
  "Sum contributions from each window. No normalization."
  r = np.zeros(nskip * (len(stft_windows) - 1) + nfft)
  h = hamming(nfft)
  for w, w_0 in zip(stft_windows, xrange(0, r.size, nskip)):
    r[w_0 : w_0+nfft] += irfft(w, nfft) * h
  return r
示例#18
0
	def hamming(self, cutoff):
		"""Filter the data using a hamming filter and store the values in filteredseries"""
		for i in range(len(self.recarray[0])):
			fil = signal.hamming(cutoff)
			output = signal.convolve(self.recarray[:,i], fil, mode='same')
			self.filteredseries[:,i] = output
		return self.filteredseries
示例#19
0
    def __window_data(data):
        # Apply window function to the decoded data & store as new key:value pair in dictionary
        # Parameters: data: [{'frame_data': string,
        #                     'frame_count': int,
        #                     'frame_time': float,
        #                     'frame_position': int,
        #                     'frame_decoded': numpy.ndarray}, ...]

        # cache window function
        if 'hann' == config_analysis.frame_window:
            window = signal.hann(config_audio.frames_per_buffer)
        elif 'hamming' == config_analysis.frame_window:
            window = signal.hamming(config_audio.frames_per_buffer)
        elif 'blackman' == config_analysis.frame_window:
            window = signal.blackman(config_audio.frames_per_buffer)
        elif 'bartlett' == config_analysis.frame_window:
            window = signal.bartlett(config_audio.frames_per_buffer)
        elif 'barthann' == config_analysis.frame_window:
            window = signal.barthann(config_audio.frames_per_buffer)
        else:
            # window function unavailable
            return

        # apply specified window function in config
        for i in range(len(data)):
            data[i]['frame_windowed'] = data[i]['frame_decoded'][:] * window
示例#20
0
	def freqAnalyze(self):
		print "freqAnalyze"
		if len(self.speechSegment)==0 or self.isBlank==True:
			return
		self.shortTimeLinjieVector = []
		self.formant = []
		self.fftFrameAbs = []
		#短时谱的临界带特征矢量
		F = [0]
		fs = self.sampleRate/self.frameSize
		for i in range(1,19):
			m = int((i+0.53)*1960/(26.81-0.53-i))
			n = m/fs
			F.append(n)
		self.formantValue = []
		self.frameSize/2+1
		self.fftFrame = []
		#h1,f1 = signal.freqz([1,-0.98],[1])
		#cc = 0
		for frame in self.frame[:-1]:
			#窗函数
			#cc = cc+1
			f = frame*signal.hamming(self.frameSize,sym=0)
			#预加重
			#f = scipy.signal.lfilter([1,-0.97],1,f)
			fftFrame = np.fft.rfft(f)/(self.frameSize/2)
			self.fftFrame.append(fftFrame)
			fftFrameAbs = [abs(fft) for fft in fftFrame]
			self.fftFrameAbs.append(fftFrameAbs)
			#短时谱临界带特征矢量
			'''
示例#21
0
def fourier_transform_and_reconstruct(image, detrend=False, window=False,
                                      ffunc=None):
    """
    Take fourier transform, alter it, and reconstruct image.  For some
    reason this is shifting the origin by 1 pixel after reconstruction, which
    should not happen.

    :param image: data
    :type image: :py:class:`numpy.ndarray`

    :param ffunc: function that alters FFT matrix
    :type ffunc: func

    :return: modified image data
    :rtype: :py:class:`numpy.ndarray`
    """
    if window:
        w = signal.hamming(image.shape)
    else:
        w = np.ones_like(image)

    if detrend:
        f = fftpack.fft(w * signal.detrend(image))
    else:
        f = fftpack.fft(w * image)

    # alter the fft
    if not ffunc is None:
        f = ffunc(f)

    result = np.fliplr(fftpack.fft(f))

    return result > result.mean()
	def get_mfcc_means(cough_wavs):
		"""
		Calculate the means of the 13 MFCCs
		over all the coughs in cough_wavs

		"""

		# Get audio of all coughs in recording
		audio_data = [read(wav)[1] for wav in cough_wavs]
		# Apply hamming window
		audio_H = [wav*hamming(len(wav)) for wav in audio_data]

		audio_H = np.array([item for sublist in audio_H for item in sublist])
		audio_data = np.array([item for sublist in audio_data for item in sublist])

		plt.subplot(2,1,1)
		plt.plot(audio_data)
		plt.subplot(2,1,2)
		plt.plot(audio_H)
		plt.show()
		exit()

		mfccs = librosa.feature.mfcc(audio_data, sr=fs, n_mfcc=13, n_fft =h, hop_length=r)
		means = list(np.mean(mfccs,axis=1))
		return means
示例#23
0
文件: utils.py 项目: gboyes/pydbm
    def mfcc(self, x, Fs, win, hop, nfft, CC):
        '''Mel-Frequency cepstral coefficients of a signal
           x := signal
           Fs := sampling rate
           win := window size
           hop := hop size
           nfft := fft size
           CC := number of coefficients'''

        #filterbank parameters
        lowestFrequency = 133.3333
        linearFilters = 13
        linearSpacing = 200./3
        logFilters = 27
        logSpacing = 1.0711703
        totalFilters = linearFilters + logFilters

        w = sig.hamming(win)

        seg = np.zeros((np.ceil(len(x)/float(hop)), win))
        i = 0
        pin = 0
        pend = len(x) - win
        while pin < pend:
            seg[i, 0:win] = x[pin:pin+win] * w
            i += 1
            pin += hop
     
        preEmp = sig.lfilter(np.array([1, -.97]), 1, seg)
        fbank = self.trfbank(Fs, nfft, lowestFrequency, linearSpacing, logSpacing, linearFilters, logFilters)[0]
        fftMag = np.abs(fftpack.fft(preEmp, nfft, axis=-1))
        earMag = np.log10(np.inner(fftMag, fbank) + 0.0000000001)
        ceps = fftpack.realtransforms.dct(earMag, type=2, norm='ortho', axis=-1)[:, 0:CC]
        
        return ceps, earMag
def spectrogramme_inv(Spectro,Phase,Fs,Tau,hopsize,win):
    
    X = Spectro * np.exp(1j*Phase)
    [Nf,Nt] = X.shape
    Nw=np.round(Tau * Fs)
    R = np.round(hopsize * Nw) 

    if win=='h':
        w = signal.hamming(Nw, sym=True)
    elif win=='b':
        w = signal.blackman(Nw, sym=True)
    else:
        w = np.ones(Nw)
    
    I = np.round(hopsize * Nw)                       
    M = 2 * (Nf-1)                       
    L = np.ceil( (Nt-1)*I + M )
    
    y = np.zeros([L,1])
         
    aux_window = ola(np.power(w,2),R, Nt)
    window=np.sqrt((1/np.max(aux_window))*np.power(w,2))
    
    y_aux=np.zeros([Nw,1])

    for r in range(0, Nt):
        
        deb = (r)*R 
        fin = deb + Nw   
        Xtilde_inv = ifft(X[:,r])
        y_aux[:,0] = Xtilde_inv*window
        y[deb:fin,0]=y[deb:fin,0]+y_aux[:,0]
        
    return y
示例#25
0
def dip(peak, smooth=True):
    """ Run a dip test. 

    The diptest can be used to test if a distribution is unimodal. In order to
    get it to work, I have to turn the peak signal into a distribution by
    simulating, and then run the test on the simulated data. This is a little
    hackish, there is probably a better/faster way.
    
    """

    # Smooth distribution using hamming
    if smooth:
        smooth = signal.convolve(peak, signal.hamming(10))
    else:
        smooth = peak

    # Set up x's
    x_grid = np.arange(0, smooth.shape[0])

    # Normalize the peak section to sum to 1
    norm = smooth / smooth.sum()

    # Simulate data from the peak distribution
    sim = choice(x_grid, size=3000, replace=True, p=norm)

    # Run diptest
    test, pval = diptest(sim)

    return test, pval
示例#26
0
def get_mfcc(path):
    """Finds the MFCCs and FFTs of a WAVE file.

  Args:
    path: The path to a WAVE file.

  Returns:
    A tuple of two iterables, the FFTs and MFCCs of the frames of the
    WAVE file.
  """
    global COMP_FRAME_SIZE
    # Read the file, and determine its length in frames
    (sample, data) = utils.read_wave_from_file(path)
    total_frames = (data.size / sample) / COMP_FRAME_SIZE

    step = COMP_FRAME_SIZE * sample
    window = hamming(step)

    # Allocate space for the FFT decompositions of each frame of sound data
    fft_out = []
    mfcc_out = []

    # Loop invariant:
    #   0 <= frame_index <= total_frames
    #   results in an array (fft_out) of FFTs that correspond to the
    #    frames of the WAVE file
    filterbank_cache = {}
    frame_index = 0

    while frame_index + (1 - FRAME_OVERLAP_FACTOR) < total_frames:
        # Obtain the frame_indexth frame from the data
        frame = data[frame_index * step : (frame_index + 1) * step]

        # Generate the FFT of the frame windowed by the hamming window
        frame_fft = numpy.fft.rfft(frame * window, n=256)
        frame_fft[frame_fft == 0] = 0.000003
        nfft = len(frame_fft)

        # Compute the mel triangular filterbank or get a cached version
        fb_key = (sample, nfft)
        if fb_key in filterbank_cache:
            filterbank = filterbank_cache[fb_key]
        else:
            filterbank = triangular_filters(sample, nfft).T
            filterbank[filterbank == 0] = 0.00003
            filterbank_cache[fb_key] = filterbank

        # The power spectrum of the frame
        power_spectrum = numpy.abs(frame_fft)
        # Filtered by the mel filterbank
        mel_power_spectrum = numpy.log10(numpy.dot(power_spectrum, filterbank))
        # With the discrete cosine transform to find the cepstrum
        cepstrum = dct(mel_power_spectrum, type=2, norm="ortho", axis=-1)

        fft_out.append(frame_fft)
        mfcc_out.append(cepstrum[: int(len(cepstrum) * SIGNIFICANT_MFCC)])
        frame_index = frame_index + FRAME_OVERLAP_FACTOR

    return numpy.array(mfcc_out)
示例#27
0
 def get_ir(self, sz, wd=50):
     x = fftfreq2(sz, 1.0/self.rate)
     ir = np.real(fft.ifft(mps(spectrum2fft(self(x)),0,0),axis=0))
     if wd:
         w = sig.hamming(2*wd)[-wd:]
         for i in range(ir.shape[1]):
             ir[-wd:,i] *= w
     return ir
示例#28
0
文件: cohen.py 项目: markyoder/pytftb
 def _make_window(self):
     hlength = np.floor(self.n_fbins / 4.0)
     if hlength % 2 == 0:
         hlength += 1
     from scipy.signal import hamming
     fwindow = hamming(hlength)
     lh = (fwindow.shape[0] - 1) / 2
     return fwindow / fwindow[lh]
示例#29
0
def srmr(x, fs, n_cochlear_filters=23, low_freq=125, min_cf=4, max_cf=128, fast=True, norm=False):
    wLengthS = .256
    wIncS = .064
    # Computing gammatone envelopes
    if fast:
        mfs = 400.0
        gt_env = fft_gtgram(x, fs, 0.010, 0.0025, n_cochlear_filters, low_freq)
    else:
        cfs = centre_freqs(fs, n_cochlear_filters, low_freq)
        fcoefs = make_erb_filters(fs, cfs)
        gt_env = np.abs(hilbert(erb_filterbank(x, fcoefs)))
        mfs = fs

    wLength = np.ceil(wLengthS*mfs)
    wInc = np.ceil(wIncS*mfs)

    # Computing modulation filterbank with Q = 2 and 8 channels
    mod_filter_cfs = compute_modulation_cfs(min_cf, max_cf, 8)
    MF = modulation_filterbank(mod_filter_cfs, mfs, 2)

    n_frames = 1 + (gt_env.shape[1] - wLength)//wInc
    w = hamming(wLength+1)[:-1] # window is periodic, not symmetric

    energy = np.zeros((n_cochlear_filters, 8, n_frames))
    for i, ac_ch in enumerate(gt_env):
        mod_out = modfilt(MF, ac_ch)
        for j, mod_ch in enumerate(mod_out):
            mod_out_frame = segment_axis(mod_ch, wLength, overlap=wLength-wInc, end='pad')
            energy[i,j,:] = np.sum((w*mod_out_frame[:n_frames])**2, axis=1)

    if norm:
        energy = normalize_energy(energy)

    erbs = np.flipud(calc_erbs(low_freq, fs, n_cochlear_filters))

    avg_energy = np.mean(energy, axis=2)
    total_energy = np.sum(avg_energy)

    AC_energy = np.sum(avg_energy, axis=1)
    AC_perc = AC_energy*100/total_energy

    AC_perc_cumsum=np.cumsum(np.flipud(AC_perc))
    K90perc_idx = np.where(AC_perc_cumsum>90)[0][0]

    BW = erbs[K90perc_idx]

    cutoffs = calc_cutoffs(mod_filter_cfs, fs, 2)[0]

    if (BW > cutoffs[4]) and (BW < cutoffs[5]):
        Kstar=5
    elif (BW > cutoffs[5]) and (BW < cutoffs[6]):
        Kstar=6
    elif (BW > cutoffs[6]) and (BW < cutoffs[7]):
        Kstar=7
    elif (BW > cutoffs[7]):
        Kstar=8

    return np.sum(avg_energy[:, :4])/np.sum(avg_energy[:, 4:Kstar]), energy
示例#30
0
 def _conv_window_fn(self, sig):
     
     duration = 1.0 /  sig.get_rate() *  sig.get_signal_length()
     base_duration =  0.2
     window_width = 15.0 *  duration / base_duration
     #sigma = 3.0  *  duration / base_duration
     #conv_window = spsignal.general_gaussian(window_width, 1, sigma)
     conv_window = spsignal.hamming(window_width, False)
     return conv_window
示例#31
0

ani = animation.FuncAnimation(fig,
                              plot_update,
                              init_func=plot_init,
                              frames=1,
                              interval=30,
                              blit=True)

q = queue.Queue()

#rtlsdr
sdr = RtlSdr()

#processing block
window = signal.hamming(SIZE)


def read_data_thread(q, ad_rdy_ev):
    global rt_data, fft_data

    #while stream.is_active():
    while 1:
        ad_rdy_ev.wait(timeout=1000)
        if not q.empty():
            #process audio data here
            data = q.get()
            #print('  signal mean:', sum(data)/len(data))
            while not q.empty():
                q.get()
            rt_data = np.real(data)
示例#32
0
def data_feeder_training(window_size, fft_size, hop_size, seq_length,
                         context_length, batch_size, files_per_pass, debug):
    """Provides an iterator over the training examples.

    :param window_size: The window size to be used for the time-frequency transformation.
    :type window_size: int
    :param fft_size: The size of the FFT in samples.
    :type fft_size: int
    :param hop_size: The hop size in samples.
    :type hop_size: int
    :param seq_length: The sequence length in frames.
    :type seq_length: int
    :param context_length: The context length in frames.
    :type context_length: int
    :param batch_size: The batch size.
    :type batch_size: int
    :param files_per_pass: How many files per pass.
    :type files_per_pass: int
    :param batch_size: The batch size.
    :type batch_size: int
    :param files_per_pass: How many files per pass.
    :type files_per_pass: int
    :param debug: A flag to indicate debug
    :type debug: bool
    :return: An iterator that will provide the input and target values.\
             The iterator yields (input, target) values.
    :rtype: callable
    """
    mixtures_list, sources_list = _get_files_lists('training')
    hamming_window = signal.hamming(window_size, True)

    def epoch_it():
        for index in range(int(len(mixtures_list) / files_per_pass)):
            mix, voice_true = _get_data_training(current_set=index + 1,
                                                 set_size=files_per_pass,
                                                 mixtures_list=mixtures_list,
                                                 sources_list=sources_list,
                                                 window_values=hamming_window,
                                                 fft_size=fft_size,
                                                 hop=hop_size,
                                                 seq_length=seq_length,
                                                 context_length=context_length,
                                                 batch_size=batch_size)

            shuffled_indices = np.random.permutation(mix.shape[0])

            mix = mix[shuffled_indices]
            voice_true = voice_true[shuffled_indices]

            for batch in range(int(mix.shape[0] / batch_size)):

                b_start = batch * batch_size
                b_end = (batch + 1) * batch_size

                mix_batch = mix[b_start:b_end, :, :]
                voice_true_batch = voice_true[
                    b_start:b_end, context_length:-context_length, :]

                yield mix_batch, voice_true_batch

                if debug:
                    break

            if debug:
                break

    return epoch_it
示例#33
0
t = -100
nH = 40
minf0 = 420
maxf0 = 460
f0et = 5
maxnpeaksTwm = 5
minSineDur = .1
harmDevSlope = 0.01
Ns = 512
H = Ns / 4

mX, pX = STFT.stftAnal(x, fs, w, N, H)
hfreq, hmag, hphase = HM.harmonicModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0,
                                           f0et, harmDevSlope, minSineDur)
xr = UF.sineSubtraction(x, Ns, H, hfreq, hmag, hphase, fs)
mXr, pXr = STFT.stftAnal(xr, fs, hamming(Ns), Ns, H)

maxplotfreq = 5000.0
plt.figure(1, figsize=(9, 7))

plt.subplot(221)
numFrames = int(mX[:, 0].size)
frmTime = H * np.arange(numFrames) / float(fs)
binFreq = fs * np.arange(N * maxplotfreq / fs) / N
plt.pcolormesh(frmTime, binFreq,
               np.transpose(mX[:, :N * maxplotfreq / fs + 1]))
plt.autoscale(tight=True)

harms = hfreq * np.less(hfreq, maxplotfreq)
harms[harms == 0] = np.nan
numFrames = int(harms[:, 0].size)
示例#34
0
def normhamming(fft_len):
    win = numpy.sqrt(hamming(fft_len, False))
    win = win / numpy.sqrt(
        numpy.sum(numpy.power(win[0:fft_len:FRAME_SHIFT], 2)))
    return win
示例#35
0
    while pin < pend:
        mX = spectrum(window(x[pin:pin + N]))
        f0t = pitchYin(mX)
        f0 = np.append(f0, f0t[0])
        pin += H
    return f0


if __name__ == '__main__':
    (fs, x) = UF.wavread('../../../sounds/bendir.wav')

    plt.figure(1, figsize=(9, 7))
    N = 2048
    H = 256
    w = hamming(2048)
    mX, pX = STFT.stftAnal(x, fs, w, N, H)
    maxplotfreq = 2000.0
    frmTime = H * np.arange(mX[:, 0].size) / float(fs)
    binFreq = fs * np.arange(N * maxplotfreq / fs) / N
    plt.pcolormesh(frmTime, binFreq,
                   np.transpose(mX[:, :N * maxplotfreq / fs + 1]))

    N = 2048
    minf0 = 130
    maxf0 = 300
    H = 256
    f0 = f0Yin(x, N, H, minf0, maxf0)
    yf0 = UF.sinewaveSynth(f0, .8, H, fs)
    frmTime = H * np.arange(f0.size) / float(fs)
    plt.plot(frmTime, f0, linewidth=2, color='k')
示例#36
0
 def hamming_win(self, array_size, *args, **kwargs):
     return signal.hamming(array_size)
示例#37
0
def smoothed_pseudo_wigner_ville(signal, timestamps=None, n_fbins=None,
                                 twindow=None, fwindow=None):
    """smoothed_pseudo_wigner_ville

    :param signal:
    :param timestamps:
    :param n_fbins:
    :param twindow:
    :param fwindow:
    :type signal:
    :type timestamps:
    :type n_fbins:
    :type twindow:
    :type fwindow:

    :return:
    :rtype:
    """
    xrow = signal.shape[0]

    if timestamps is None:
        timestamps = np.arange(signal.shape[0])
    if n_fbins is None:
        n_fbins = signal.shape[0]
    if fwindow is None:
        hlength = np.floor(n_fbins / 4.0)
        hlength += 1 - (hlength % 2)
        fwindow = ssig.hamming(hlength)
    elif fwindow.shape[0] % 2 == 0:
        raise ValueError('The smoothing window must have an odd length.')
    lh = (fwindow.shape[0] - 1) / 2

    if twindow is None:
        glength = np.floor(n_fbins / 4.0)
        glength += 1 - (glength % 2)
        twindow = ssig.hamming(glength)
    elif twindow.shape[0] % 2 == 0:
        raise ValueError('The smoothing window must have an odd length.')
    lg = (twindow.shape[0] - 1) / 2

    tcol = timestamps.shape[0]
    deltat = timestamps[1:] - timestamps[:-1]
    if deltat.min() != deltat.max():
        raise ValueError("Time instants must be regularly sampled.")
    else:
        dt = deltat.min()

    tfr = np.zeros((n_fbins, tcol), dtype=complex)
    tf2 = np.zeros((n_fbins, tcol), dtype=complex)
    tf3 = np.zeros((n_fbins, tcol), dtype=complex)
    dh = derive_window(fwindow)

    for icol in range(tcol):
        ti = timestamps[icol]
        taumax = min([ti + lg - 1, xrow - ti + lg, np.round(n_fbins / 2.0) - 1,
                      lh])
        points = np.arange(-min([lg, xrow - ti]), min([lg, ti - 1]) + 1)
        g2 = twindow[lg + points]
        g2 = g2 / g2.sum()
        tg2 = g2 * points
        xx = signal[ti - 1 - points] * np.conj(signal[ti - 1 - points])
        tfr[0, icol] = (g2 * xx).sum()
        tf2[0, icol] = (tg2 * xx).sum()
        tf3[0, icol] = dh[lh + 1] * tfr[0, icol]

        for tau in range(int(taumax)):
            points = np.arange(-min([lg, xrow - ti - tau]),
                               min([lg, ti - tau - 1]) + 1)
            g2 = twindow[lg + points]
            g2 = g2 / g2.sum()
            tg2 = g2 * points
            xx = signal[ti + tau - 1 - points] * np.conj(signal[ti - tau - 1 - points])
            tfr[tau, icol] = (g2 * xx).sum() * fwindow[lh + tau]
            tf2[tau, icol] = fwindow[lh + tau] * (tg2 * xx).sum()
            tf3[tau, icol] = dh[lh + tau] * (g2 * xx).sum()
            tfr[n_fbins - tau - 1, icol] = (g2 * np.conj(xx)).sum() * fwindow[lh - tau]
            tf2[n_fbins - tau - 1, icol] = (tg2 * np.conj(xx)).sum() * fwindow[lh - tau]
            tf3[n_fbins - tau - 1, icol] = dh[lh - tau] * (g2 * np.conj(xx)).sum()

    tfr = np.real(np.fft.fft(tfr, axis=0)).ravel()
    tf2 = np.real(np.fft.fft(tf2, axis=0)).ravel()
    tf3 = np.imag(np.fft.fft(tf3, axis=0)).ravel()

    no_warn_mask = tfr != 0
    tf2[no_warn_mask] = np.round(tf2[no_warn_mask] / tfr[no_warn_mask] / dt)
    tf3[no_warn_mask] = np.round(
        n_fbins * tf3[no_warn_mask] / tfr[no_warn_mask] / (2 * np.pi))
    tfr, tf2, tf3 = [x.reshape(n_fbins, tcol).astype(complex) for x in (tfr, tf2, tf3)]
    tf3 = np.real(tf3)

    rtfr = np.zeros((n_fbins, tcol), dtype=complex)
    ex = np.mean(np.abs(signal) ** 2)
    threshold = ex * 1.0e-6

    for icol in range(tcol):
        for jcol in range(n_fbins):
            if np.abs(tfr[jcol, icol]) > threshold:
                icolhat = min(max([icol - tf2[jcol, icol], 1]), tcol)
                jcolhat = jcol - tf3[jcol, icol]
                jcolhat = (((int(jcolhat) - 1) % n_fbins) + n_fbins) % n_fbins + 1
                rtfr[jcol, icol] += tfr[jcol, icol]
                tf2[jcol, icol] = jcolhat + 1j * icolhat
            else:
                tf2[jcol, icol] = np.inf * (1 + 1j)
                rtfr[jcol, icol] += tfr[jcol, icol]

    return tfr, rtfr, tf2
示例#38
0
def pseudo_wigner_ville(signal, timestamps=None, n_fbins=None, fwindow=None):
    """pseudo_wigner_ville

    :param signal:
    :param timestamps:
    :param n_fbins:
    :param fwindow:
    :type signal:
    :type timestamps:
    :type n_fbins:
    :type fwindow:
:return:
:rtype:
    """
    xrow = signal.shape[0]
    if timestamps is None:
        timestamps = np.arange(signal.shape[0])
    if n_fbins is None:
        n_fbins = signal.shape[0]
    tcol = timestamps.shape[0]

    if fwindow is None:
        hlength = np.floor(n_fbins / 4.0)
        if hlength % 2 == 0:
            hlength += 1
        fwindow = ssig.hamming(int(hlength))
    elif fwindow.shape[0] % 2 == 0:
        raise ValueError('The smoothing fwindow must have an odd length.')
    lh = (fwindow.shape[0] - 1) // 2
    fwindow = fwindow / fwindow[lh]

    tfr = np.zeros((n_fbins, tcol), dtype=complex)
    tf2 = np.zeros((n_fbins, tcol), dtype=complex)
    dh = derive_window(fwindow)
    for icol in range(tcol):
        ti = timestamps[icol]
        taumax = min([ti - 1, xrow - ti, np.round(n_fbins / 2.0) - 1, lh])
        tau = np.arange(-taumax, taumax + 1)
        indices = np.remainder(n_fbins + tau, n_fbins) + 1
        tfr[indices - 1, icol] = fwindow[lh + tau] * signal[ti + tau - 1] * \
            np.conj(signal[ti - tau - 1])
        tf2[indices - 1, icol] = dh[lh + tau] * signal[ti + tau - 1] * \
            np.conj(signal[ti - tau - 1])
        tau = np.round(n_fbins / 2)
        if (ti <= (xrow - tau)) and (ti > (tau + 1)) and (tau <= lh):
            _x = fwindow[lh + 1 + tau] * signal[ti + tau] * np.conj(signal[ti - tau])
            _y = fwindow[lh + 1 - tau] * signal[ti - tau] * np.conj(signal[ti + tau])
            tfr[tau + 1, icol] = (_x + _y) * 0.5
            _x = dh[lh + 1 + tau] * signal[ti + tau] * np.conj(signal[ti - tau])
            _y = dh[lh + 1 - tau] * signal[ti - tau] * np.conj(signal[ti + tau])
            tf2[tau + 1, icol] = (_x + _y) * 0.5
    tfr = np.real(np.fft.fft(tfr, axis=0))
    tf2 = np.imag(np.fft.fft(tf2, axis=0))
    tfr = tfr.ravel()
    tf2 = tf2.ravel()
    no_warn_mask = tfr != 0
    tf2[no_warn_mask] *= n_fbins / tfr[no_warn_mask] / (2 * np.pi)
    tf2[no_warn_mask] = np.round(tf2[no_warn_mask])
    tfr = tfr.reshape(n_fbins, tcol)
    tf2 = tf2.reshape(n_fbins, tcol)

    rtfr = np.zeros((n_fbins, tcol), dtype=complex)
    tmin = timestamps.min()
    tmax = timestamps.max()
    threshold = 1.0e-6 * (np.abs(signal[tmin:(tmax + 1)]) ** 2).mean()

    for icol in range(tcol):
        for jcol in range(n_fbins):
            if np.abs(tfr[jcol, icol]) > threshold:
                jcolhat = jcol - int(tf2[jcol, icol])
                jcolhat = (((jcolhat - 1) % n_fbins) + n_fbins) % n_fbins
                jcolhat += 1
                rtfr[jcolhat - 1, icol] += tfr[jcol, icol]
                tf2[jcol, icol] = jcolhat
            else:
                tf2[jcol, icol] = np.inf
                rtfr[jcol, icol] += tfr[jcol, icol]

    return tfr, rtfr, tf2
示例#39
0
    def get_data(self):
        """Get spectrogram data from the tuner.  Will return width number of
		values which are the intensities of each frequency bucket (i.e. FFT of
		radio samples).
		"""
        # Get width number of raw samples so the number of frequency bins is
        # the same as the display width.  Add two because there will be mean/DC
        # values in the results which are ignored. Increase by 1/self.zoom_fac if needed

        if self.zoom_fac < (self.sdr.sample_rate / 1000000):
            zoom = int(self.width *
                       ((self.sdr.sample_rate / 1000000) / self.zoom_fac))
        else:
            zoom = self.width
            self.zoom_fac = self.get_sample_rate()

        if zoom < freqshow.SDR_SAMPLE_SIZE:
            freqbins = self.sdr.read_samples(freqshow.SDR_SAMPLE_SIZE)[0:zoom +
                                                                       2]
        else:
            zoom = self.width
            self.zoom_fac = self.get_sample_rate()
            freqbins = self.sdr.read_samples(freqshow.SDR_SAMPLE_SIZE)[0:zoom +
                                                                       2]

        # Apply a window function to the sample to remove power in sample sidebands before the fft.

        if self.filter == 'kaiser':
            window = signal.kaiser(
                freqshow.SDR_SAMPLE_SIZE,
                self.kaiser_beta,
                False,
            )[0:zoom +
              2]  # for every bin there is a window the same exact size as the read samples.
        elif self.filter == 'boxcar':
            window = signal.boxcar(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'hann':
            window = signal.hann(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'hamming':
            window = signal.hamming(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'blackman':
            window = signal.blackman(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'blackmanharris':
            window = signal.blackmanharris(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'bartlett':
            window = signal.bartlett(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'barthann':
            window = signal.barthann(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        elif self.filter == 'nuttall':
            window = signal.nuttall(
                freqshow.SDR_SAMPLE_SIZE,
                False,
            )[0:zoom + 2]
        else:
            window = 1

        samples = freqbins * window

        # Run an FFT and take the absolute value to get frequency magnitudes.
        freqs = np.absolute(fft(samples))

        # Ignore the mean/DC values at the ends.
        freqs = freqs[1:-1]

        # Reverse the order of the freqs array if swaping I and Q
        if self.swap_iq == True:
            freqs = freqs[::-1]

        # Shift FFT result positions to put center frequency in center.
        freqs = np.fft.fftshift(freqs)

        # Truncate the freqs array to the width of the screen if neccesary.
        if freqs.size > self.width:

            freq_step = self.get_freq_step(
            )  # Get the frequency step in Hz between pixels.
            shiftsweep = int(self.get_lo_offset() * 1000000 /
                             freq_step)  # LO offset in pixels.
            extra_samples = int(
                (freqs.size - self.width) / 2
            )  # The excess samples either side of the display width in pixels.

            if extra_samples > abs(
                    shiftsweep
            ):  # check if there is room to shift the array by the LO offset.

                if self.get_swap_iq() == True:
                    lextra = extra_samples + shiftsweep
                elif self.get_swap_iq() == False:
                    lextra = extra_samples - shiftsweep
            else:
                lextra = extra_samples

            rextra = freqs.size - (lextra + self.width)
            freqs = freqs[lextra:-rextra]

        # Convert to decibels.
        freqs = 20.0 * np.log10(freqs)

        # Get signal strength of the center frequency.

        #		for i in range ( 1, 11):
        #			self.sig_strength = (self.get_sig_strength() + freqs[((zoom+2)/2)+i-5])
        #		self.sig_strength = self.get_sig_strength()/10

        # Update model's min and max intensities when auto scaling each value.
        if self.min_auto_scale:
            min_intensity = np.min(freqs)
            self.min_intensity = min_intensity if self.min_intensity is None \
             else min(min_intensity, self.min_intensity)
        if self.max_auto_scale:
            max_intensity = np.max(freqs)
            self.max_intensity = max_intensity if self.max_intensity is None \
             else max(max_intensity, self.max_intensity)
        # Update intensity range (length between min and max intensity).
        self.range = self.max_intensity - self.min_intensity

        # Return frequency intensities.
        return freqs
示例#40
0
def lin_error(enr=10.0,txlen=1000.0,n_ipp=10,ipp=20e-3,bw=1e6,dr=10.0,ddop=1.0,sr=100e6,plot=False):
    '''
     Determine linearized errors for range and range-rate error
     for a psuedorandom binary phase coded radar transmit pulse
     with a certain transmit bandwidth (inverse of bit length)

     calculate line of sight range and range-rate error,
     given ENR after coherent integration (pulse compression)
     txlen in microseconds.

    Simulate a measurement and do a linearized error estimate.

    '''
    codes=[]
    t_vecs=[]    
    n_bits = int(bw*txlen/1e6)
    oversample=int(sr/bw)
    wfun=ss.hamming(oversample)
    wfun=wfun/n.sum(wfun)
    for i in range(n_ipp):
        bits=n.array(n.sign(n.random.randn(n_bits)),dtype=n.complex64)
        zcode=n.zeros(n_bits*oversample+2*oversample,dtype=n.complex64)
        for j in range(oversample):
            zcode[n.arange(n_bits)*oversample+j+oversample]=bits
        # filter signal so that phase transitions are not too sharp
        zcode=n.convolve(wfun,zcode,mode="same")
        codes.append(zcode)
        tcode=n.arange(n_bits*oversample+2*oversample)/sr + float(i)*ipp
        t_vecs.append(tcode)

    z0=simulate_echo(codes,t_vecs,dop_Hz=0.0,range_m=0.0,bw=bw,sr=sr)
    tau=(float(n_ipp)*txlen/1e6)
    
    # convert coherently integrated ENR to SNR (variance of the measurement errors at receiver bandwidth)
    snr = enr/(tau*sr)

    if plot:
        plt.plot(z0.real)
        plt.plot(z0.imag)
        plt.title("Echo")
        plt.show()

    z_dr=simulate_echo(codes,t_vecs,dop_Hz=0.0,range_m=dr,bw=bw,sr=sr)
    z_diff_r=(z0-z_dr)/dr    
    if plot:
        plt.plot(z_diff_r.real)
        plt.plot(z_diff_r.imag)
        plt.title("Range derivative of echo (dm/dr)")
        plt.show()
    z_ddop=simulate_echo(codes,t_vecs,dop_Hz=ddop,range_m=0.0,bw=bw,sr=sr)
    z_diff_dop=(z0-z_ddop)/ddop
    
    if plot:
        plt.plot(z_diff_dop.real)
        plt.plot(z_diff_dop.imag)
        plt.title("Doppler derivative of echo (dm/df)")        
        plt.show()

    t_l=len(z_dr)
    A=n.zeros([t_l,2],dtype=n.complex64)
    A[:,0]=z_diff_r
    A[:,1]=z_diff_dop
    S=n.real(n.linalg.inv(n.dot(n.transpose(n.conj(A)),A))/snr)

    return(n.sqrt(n.diag(S)))
示例#41
0
def mfcc(input, nwin=256, nfft=512, fs=16000, nceps=13):
    """Compute Mel Frequency Cepstral Coefficients.

    Parameters
    ----------
    input: ndarray
        input from which the coefficients are computed

    Returns
    -------
    ceps: ndarray
        Mel-cepstrum coefficients
    mspec: ndarray
        Log-spectrum in the mel-domain.

    Notes
    -----
    MFCC are computed as follows:
        * Pre-processing in time-domain (pre-emphasizing)
        * Compute the spectrum amplitude by windowing with a Hamming window
        * Filter the signal in the spectral domain with a triangular
        filter-bank, whose filters are approximatively linearly spaced on the
        mel scale, and have equal bandwith in the mel scale
        * Compute the DCT of the log-spectrum

    References
    ----------
    .. [1] S.B. Davis and P. Mermelstein, "Comparison of parametric
           representations for monosyllabic word recognition in continuously
           spoken sentences", IEEE Trans. Acoustics. Speech, Signal Proc.
           ASSP-28 (4): 357-366, August 1980."""

    # MFCC parameters: taken from auditory toolbox
    over = nwin - 160
    # Pre-emphasis factor (to take into account the -6dB/octave rolloff of the
    # radiation at the lips level)
    prefac = 0.97

    #lowfreq = 400 / 3.
    lowfreq = 133.33
    #highfreq = 6855.4976
    linsc = 200 / 3.
    logsc = 1.0711703

    nlinfil = 13
    nlogfil = 27
    nfil = nlinfil + nlogfil

    w = hamming(nwin, sym=0)

    fbank = trfbank(fs, nfft, lowfreq, linsc, logsc, nlinfil, nlogfil)[0]

    #------------------
    # Compute the MFCC
    #------------------
    extract = lfilter([1., -prefac], 1, input)
    framed = segment_axis(extract, nwin, over) * w

    # Compute the spectrum magnitude
    spec = np.abs(fft(framed, nfft, axis=-1))
    # Filter the spectrum through the triangle filterbank
    mspec = np.log10(np.dot(spec, fbank.T))
    # Use the DCT to 'compress' the coefficients (spectrum -> cepstrum domain)
    ceps = dct(mspec, type=2, norm='ortho', axis=-1)[:, :nceps]

    return ceps, mspec, spec
def sineModelMultiRes(inputFile="../../sounds/orchestra.wav", 
                    windows=(signal.blackman(4095), signal.hamming(2047), np.hamming(1023)),
                    Ns=(4096, 2048, 1024), 
                    Bs=(1000, 5000, 22050), 
                    t=-80, minSineDur=0.02, 
                    maxnSines=150, freqDevOffset=10, freqDevSlope=0.001, PlotIt=True):
    
    sN = 512
    H = sN/4
    (fs, x) = UF.wavread(inputFile)
    
    tfreq, tmag, tphase = sineModelMultiResAnal(x, fs, windows, Ns, Bs, H, t, 
                            minSineDur, maxnSines, freqDevOffset, freqDevSlope)
    y = SM.sineModelSynth(tfreq, tmag, tphase, sN, H, fs)
    
    # calculate diff between x & y
    diffLength = min([x.size, y.size])
    diff = np.abs(x[:diffLength] - y[:diffLength])
    print("diff {0}".format(np.sum(diff)))
    
    outputFile = os.path.basename(inputFile)[:-4] + '_sineModelMulti.wav'
    UF.wavwrite(y, fs, outputFile)
    
    if not PlotIt:
        return
    
    plt.figure(figsize=(12, 9))

    maxplotfreq = 10000.0

    # plot the input sound
    plt.subplot(3,1,1)
    plt.plot(np.arange(x.size)/float(fs), x)
    plt.axis([0, x.size/float(fs), min(x), max(x)])
    plt.ylabel('amplitude')
    plt.xlabel('time (sec)')
    plt.title('input sound: x')

    # plot the sinusoidal frequencies
    plt.subplot(3,1,2)
    if (tfreq.shape[1] > 0):
        numFrames = tfreq.shape[0]
        frmTime = H*np.arange(numFrames)/float(fs)
        tfreq[tfreq<=0] = np.nan
        plt.ylabel('frequency (Hz)')
    	plt.xlabel('time (sec)')
    	plt.title('input sound: x')
        plt.plot(frmTime, tfreq)
        plt.axis([0, x.size/float(fs), 0, maxplotfreq])
        plt.title('frequencies of sinusoidal tracks')

    # plot the output sound
    plt.subplot(3,1,3)
    plt.plot(np.arange(y.size)/float(fs), y)
    plt.axis([0, y.size/float(fs), min(y), max(y)])
    plt.ylabel('amplitude')
    plt.xlabel('time (sec)')
    plt.title('output sound: y')

    plt.tight_layout()
    plt.show()
    df_EFR_label = pd.DataFrame(df_EFR_input.iloc[i, 1024:1031].values.reshape(1,7))
    df_EFR_detrend = df_EFR_detrend.append(pd.concat([df_EFR_detrend_temp, df_EFR_label], axis=1, ignore_index=True))

# set the title of columns
df_EFR_detrend.columns = np.append(np.arange(1024), ["Subject", "Sex", "Condition", "Vowel", "Sound Level", "Num", "EFR/FFR"])
df_EFR_detrend = df_EFR_detrend.reset_index(drop=True)

# df_EFR_label = pd.DataFrame(df_EFR.iloc[, 1024:1031].values.reshape(1,7))
df_EFR_detrend_label = df_EFR_input.iloc[:, 1024:1031]


# Time domain

# Define window function
win_kaiser = signal.kaiser(1024, beta=14)
win_hamming = signal.hamming(1024)


# implement the window function
df_EFR_win = pd.DataFrame()
for i in range(1408):
    # implement the window function
    df_EFR_t_win_temp = pd.DataFrame((df_EFR_detrend.iloc[i, 0:1024] * win_hamming).values.reshape(1,1024))
    df_EFR_label_temp = pd.DataFrame(df_EFR_detrend.iloc[i, 1024:1031].values.reshape(1,7))
    df_EFR_win = df_EFR_win.append(pd.concat([df_EFR_t_win_temp, df_EFR_label_temp], axis=1, ignore_index=True))
    
# set the title of columns
df_EFR = df_EFR_detrend.sort_values(by=["Condition", "Subject"])
df_EFR = df_EFR.reset_index(drop=True)
df_EFR_win.columns = np.append(np.arange(1024), ["Subject", "Sex", "Condition", "Vowel", "Sound Level", "Num", "EFR/FFR"])
df_EFR_win = df_EFR_win.sort_values(by=["Condition", "Subject"])
示例#44
0
def hamming3d(a, b, c):
    w2d = np.outer(ss.hamming(a), ss.hamming(b))
    w3d = np.zeros((a, b, c))
    for i in range(a):
        w3d[i, :, :] = np.outer(w2d[i, :].flatten(), ss.hamming(c))
    return w3d**(1.0 / 3.0)
示例#45
0
def istft(spectrum, winsize_samples, nfft_samples, overlap_percentage,
          wintype):

    hopsize_fraction = (100 - overlap_percentage) / 100
    hopsize = int(hopsize_fraction * winsize_samples)
    specsize = int(nfft_samples / 2 + 1)

    # get the synthesis window
    if wintype == 'cosine':
        win_synthesis = cosine(winsize_samples, sym=False)
    elif wintype == 'blackmanharris':
        win_synthesis = blackmanharris(winsize_samples, sym=False)
    elif wintype == 'blackman':
        win_synthesis = blackman(winsize_samples, sym=False)
    elif wintype == 'hamming':
        win_synthesis = hamming(winsize_samples, sym=False)
    elif wintype == 'hanning':
        win_synthesis = hanning(winsize_samples, sym=False)

    #make sure that the scaling is correct
    sqsum = np.sum(np.square(win_synthesis)) / winsize_samples
    invsqsum = 1 / sqsum
    tmp = hopsize_fraction
    correctionFactor = tmp * invsqsum
    win_synthesis = win_synthesis * correctionFactor

    # allocate memory for the signal
    numFrames = spectrum.shape[1]
    framesize = int((100 - overlap_percentage) * winsize_samples / 100)

    # allocate a 2D array for the time-domain signal (to be vectorized later)
    sig_hat = np.zeros(shape=(numFrames, framesize))

    # prepare buffers for the istft
    buffer_chunks = int(1 / hopsize_fraction)
    ifft_buffer = np.zeros(shape=(winsize_samples, buffer_chunks))

    for idx in range(0, numFrames):

        # get the current frame spectrum and append the complex conjugate
        tmpspec = np.zeros(shape=(nfft_samples)) + 0.j
        tmpspec[
            0:specsize] = spectrum[:,
                                   idx]  # select the column of the spectrogram
        tmpspec[specsize:nfft_samples] = np.conj(
            np.flip(spectrum[1:specsize - 1, idx], 0))

        # take the ifft of the spectrum
        tmpframe = np.real(ifft(tmpspec, nfft_samples))

        # apply the synthesis window to the time frame, and keep only the number of samples corresponding to the analysis window (if zero-padding was applied, this ensures that the irrelevant samples are discarded)
        frame_in_time = win_synthesis * tmpframe[0:winsize_samples]

        # put the last one out, take the new one in
        ifft_buffer[:, 1:buffer_chunks] = ifft_buffer[:, 0:buffer_chunks - 1]
        ifft_buffer[:, 0] = frame_in_time

        # implementation of the overlap-add procedure
        frame_out = np.zeros(shape=(hopsize))
        for bufidx in range(0, buffer_chunks):
            bufstart = bufidx * hopsize
            bufend = bufstart + hopsize
            frame_out = frame_out + ifft_buffer[bufstart:bufend, bufidx]

        # store the current output frame in the correct indices of the signal
        sig_hat[idx, :] = frame_out

        #--- end of for idx in range(0,numFrames)

    # vectorize 2D signal to obtain one time-domain signal vector
    sig_out = sig_hat.flatten()

    return sig_out
示例#46
0
def spectrogram(signal, time_samples=None, n_fbins=None, window=None):
    """Compute the spectrogram and reassigned spectrogram.

    :param signal: signal to be analzsed
    :param time_samples: time instants (default: np.arange(len(signal)))
    :param n_fbins: number of frequency bins (default: len(signal))
    :param window: frequency smoothing window (default: Hamming with \
        size=len(signal)/4)
    :type signal: array-like
    :type time_samples: array-like
    :type n_fbins: int
    :type window: array-like

    :return: spectrogram, reassigned specstrogram and matrix of reassignment \
        vectors
    :rtype: tuple(array-like)
    """
    if time_samples is None:
        time_samples = np.arange(signal.shape[0])
    elif np.unique(np.diff(time_samples)).shape[0] > 1:
        raise ValueError('Time instants must be regularly sampled.')
    if n_fbins is None:
        n_fbins = signal.shape[0]
    if window is None:
        wlength = int(np.floor(signal.shape[0] / 4.0))
        wlength += 1 - np.remainder(wlength, 2)
        window = ssig.hamming(wlength)
    elif window.shape[0] % 2 == 0:
        raise ValueError('The smoothing window must have an odd length.')

    tfr = np.zeros((n_fbins, time_samples.shape[0]), dtype=complex)
    tf2 = np.zeros((n_fbins, time_samples.shape[0]), dtype=complex)
    tf3 = np.zeros((n_fbins, time_samples.shape[0]), dtype=complex)
    lh = (window.shape[0] - 1) // 2
    th = window * np.arange(-lh, lh + 1)
    dwin = derive_window(window)

    for icol in range(time_samples.shape[0]):
        ti = time_samples[icol]
        tau = np.arange(-np.min([np.round(n_fbins / 2) - 1, lh, ti]),
                        np.min([np.round(n_fbins / 2) - 1, lh,
                                signal.shape[0] - ti]) + 1).astype(int)
        indices = np.remainder(n_fbins + tau, n_fbins)
        norm_h = np.linalg.norm(window[lh + tau], ord=2)
        tfr[indices, icol] = signal[ti + tau - 1] * np.conj(window[lh + tau]) / norm_h
        tf2[indices, icol] = signal[ti + tau - 1] * np.conj(th[lh + tau]) / norm_h
        tf3[indices, icol] = signal[ti + tau - 1] * np.conj(dwin[lh + tau]) / norm_h

    tfr = np.fft.fft(tfr, axis=0).ravel()
    tf2 = np.fft.fft(tf2, axis=0).ravel()
    tf3 = np.fft.fft(tf3, axis=0).ravel()

    no_warn_mask = tfr != 0
    tf2[no_warn_mask] = np.round(np.real(tf2[no_warn_mask] / tfr[no_warn_mask]))
    tf3[no_warn_mask] = np.round(
        np.imag(n_fbins * tf3[no_warn_mask] / tfr[no_warn_mask] / (2 * np.pi)))

    tfr = np.abs(tfr) ** 2
    tfr = tfr.reshape(n_fbins, time_samples.shape[0])
    tf2 = tf2.reshape(n_fbins, time_samples.shape[0])
    tf3 = tf3.reshape(n_fbins, time_samples.shape[0])
    tf3 = np.real(tf3)

    rtfr = np.zeros((n_fbins, time_samples.shape[0]), dtype=complex)
    ix = np.arange(time_samples.min(), time_samples.max() + 1) - 1
    threshold = 1e-6 * np.mean(np.abs(signal[ix])**2)
    for icol in range(time_samples.shape[0]):
        for jcol in range(n_fbins):
            if np.abs(tfr[jcol, icol]) > threshold:
                icolhat = icol + tf2[jcol, icol]
                icolhat = np.min([np.max([icolhat, 1]), time_samples.shape[0]])
                jcolhat = jcol - tf3[jcol, icol]
                jcolhat = (((jcolhat - 1) % n_fbins) + n_fbins) % n_fbins
                rtfr[int(jcolhat), int(icolhat) - 1] += tfr[jcol, icol]
                tf2[jcol, icol] = jcolhat + 1j * icolhat
            else:
                tf2[jcol, icol] = np.inf
                rtfr[jcol, icol] += tfr[jcol, icol]
    return tfr, rtfr, tf2
                output=False,
                frames_per_buffer=CHUNK,
                stream_callback=audio_callback)

if Recording:
    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)

print("Start Recording")
stream.start_stream()

# processing block

window = signal.hamming(CHUNK)


def read_audio_thead(q, stream, frames, ad_rdy_ev):
    global rt_data
    global fft_data

    while stream.is_active():
        ad_rdy_ev.wait(timeout=1000)
        if not q.empty():
            # process audio data here
            data = q.get()
            while not q.empty():
                q.get()
            rt_data = np.frombuffer(data, np.dtype('<i2'))
            rt_data = rt_data * window
示例#48
0
def pseudo_margenau_hill(signal, timestamps=None, n_fbins=None, fwindow=None):
    """pseudo_margenau_hill

    :param signal:
    :param timestamps:
    :param n_fbins:
    :param fwindow:
    :type signal:
    :type timestamps:
    :type n_fbins:
    :type fwindow:
:return:
:rtype:
    """
    xrow = signal.shape[0]
    if timestamps is None:
        timestamps = np.arange(signal.shape[0])
    if n_fbins is None:
        n_fbins = signal.shape[0]
    tcol = timestamps.shape[0]

    if fwindow is None:
        hlength = np.floor(n_fbins / 4.0)
        if hlength % 2 == 0:
            hlength += 1
        fwindow = ssig.hamming(hlength)
    elif fwindow.shape[0] % 2 == 0:
        raise ValueError('The smoothing fwindow must have an odd length.')
    lh = (fwindow.shape[0] - 1) / 2
    fwindow = fwindow / fwindow[lh]

    tfr = np.zeros((n_fbins, tcol), dtype=complex)
    tf2 = np.zeros((n_fbins, tcol), dtype=complex)
    dh = derive_window(fwindow)
    tfr = np.zeros((n_fbins, tcol), dtype=complex)
    for icol in range(tcol):
        ti = timestamps[icol]
        start = min([np.round(n_fbins / 2.0) - 1, lh, xrow - ti])
        end = min([np.round(n_fbins / 2.0) - 1, lh, ti - 1])
        tau = np.arange(-start, end + 1)
        indices = np.remainder(n_fbins + tau, n_fbins)
        tfr[indices, icol] = fwindow[lh + tau] * signal[ti - 1] * \
            np.conj(signal[ti - tau - 1])
        tf2[indices, icol] = dh[lh + tau] * signal[ti - 1] * \
            np.conj(signal[ti - tau - 1])

    tfr = np.fft.fft(tfr, axis=0)
    tf2 = np.fft.fft(tf2, axis=0)
    tfr = tfr.ravel()
    tf2 = tf2.ravel()
    no_warn_mask = tfr != 0
    tf2[no_warn_mask] *= n_fbins / tfr[no_warn_mask] / (2 * np.pi)
    tf2[no_warn_mask] = np.round(tf2[no_warn_mask])
    tfr = np.real(tfr)
    tf2 = np.imag(tf2)
    tfr = tfr.reshape(n_fbins, tcol)
    tf2 = tf2.reshape(n_fbins, tcol)

    rtfr = np.zeros((n_fbins, tcol), dtype=complex)
    threshold = 1.0e-6 * (np.abs(signal) ** 2).mean()

    for icol in range(tcol):
        for jcol in range(n_fbins):
            if np.abs(tfr[jcol, icol]) > threshold:
                jcolhat = jcol - tf2[jcol, icol]
                jcolhat = (((jcolhat - 1) % n_fbins) + n_fbins) % n_fbins
                jcolhat += 1
                rtfr[jcolhat - 1, icol] += tfr[jcol, icol]
                tf2[jcol, icol] = jcolhat
            else:
                tf2[jcol, icol] = np.inf
                rtfr[jcol, icol] += tfr[jcol, icol]

    return tfr, rtfr, tf2
示例#49
0
for i, t in enumerate(timestamp_array):
 try:
   a = np.load(folder + '/' + filename + '-audio-' + str(i).zfill(4) + '.npy')    # arctic_a0001-audio-0000.npy
   #if i % 10 == 1:
   #   print "wave before inverse quantization: ", i, a[0:10]
   a = inv_mulaw_quantize(a)
   #if i% 100 == 1:
   #   print "wave after inverse quantization: ", a[0:10]
   period_start = float(t) - 0.005 if float(t) - 0.005 > 0 else 0
   period_end = float(t) + 0.005 if float(t) + 0.005 < timestamp_array[-1] else timestamp_array[-1]
   frame_start = int(period_start * 1000)
   frame_end = int(period_end * 1000)
   sample_start = int(period_start * 16000)
   sample_end = int(period_end * 16000)
   window = hamming(len(a), sym=False)
   #print i, t, sample_start, sample_end, sample_end - sample_start, len(a), len(A) #, a[20:40]
   if sample_end > len(A):
      continue
   if sample_end - sample_start == 160 and len(a) == 160:
      A_abs[sample_start:sample_end] += a #* window  # Direct add
   elif sample_end - sample_start == 161 and len(a) == 160:
      A_abs[sample_start:sample_end-1] += a #* window
   elif sample_end - sample_start == 159 and len(a) == 160:
      A_abs[sample_start:sample_end+1] += a #* window
   elif sample_end - sample_start == 160 and len(a) == 161:
      window = hann(len(a)-1, sym=False)
      A_abs[sample_start:sample_end] += a[1:] #* window
   elif sample_end - sample_start == 161 and len(a) == 161:
      window = hann(len(a)-1, sym=False)
      A_abs[sample_start:sample_end-1] += a[1:] #* window
示例#50
0
def smoothed_pseudo_wigner_ville(signal,
                                 timestamps=None,
                                 freq_bins=None,
                                 twindow=None,
                                 fwindow=None):
    """Smoothed Pseudo Wigner-Ville time-frequency distribution.
    :param signal: signal to be analyzed
    :param timestamps: time instants of the signal
    :param freq_bins: number of frequency bins
    :param twindow: time smoothing window
    :param fwindow: frequency smoothing window
    :type signal: array-like
    :type timestamps: array-like
    :type freq_bins: int
    :type twindow: array-like
    :type fwindow: array-like
    :return: Smoothed pseudo Wigner Ville distribution
    :rtype: array-like
    """
    if timestamps is None:
        timestamps = np.arange(signal.shape[0])
    if freq_bins is None:
        freq_bins = signal.shape[0]

    if fwindow is None:
        winlength = np.floor(freq_bins / 4.0)
        winlength = winlength + 1 - np.remainder(winlength, 2)
        from scipy.signal import hamming
        fwindow = hamming(int(winlength))
    elif fwindow.shape[0] % 2 == 0:
        raise ValueError('The smoothing fwindow must have an odd length.')

    if twindow is None:
        timelength = np.floor(freq_bins / 10.0)
        timelength += 1 - np.remainder(timelength, 2)
        from scipy.signal import hamming
        twindow = hamming(int(timelength))
    elif twindow.shape[0] % 2 == 0:
        raise ValueError('The smoothing fwindow must have an odd length.')

    tfr = np.zeros((freq_bins, timestamps.shape[0]), dtype=complex)
    lg = (twindow.shape[0] - 1) / 2
    lh = (fwindow.shape[0] - 1) / 2
    for icol in range(timestamps.shape[0]):
        ti = timestamps[icol]
        taumax = min([
            ti + lg - 1, signal.shape[0] - ti + lg,
            np.round(freq_bins / 2.0) - 1, lh
        ])
        points = np.arange(-min([lg, signal.shape[0] - ti]),
                           min([lg, ti - 1]) + 1)
        g2 = twindow[lg + points]
        g2 = g2 / np.sum(g2)
        tfr[0, icol] = np.sum(g2 * signal[ti - points - 1] *
                              np.conj(signal[ti - points - 1]))
        for tau in range(int(taumax)):
            points = np.arange(-min([lg, signal.shape[0] - ti - tau]),
                               min([lg, ti - 1 - tau]) + 1)
            g2 = twindow[lg + points]
            g2 = g2 / np.sum(g2)
            R = np.sum(g2 * signal[ti + tau - points - 1] *
                       np.conj(signal[ti - tau - points - 1]))
            tfr[1 + tau, icol] = fwindow[lh + tau + 1] * R
            R = np.sum(g2 * signal[ti - tau - points - 1] *
                       np.conj(signal[ti + tau - points - 1]))
            tfr[freq_bins - tau - 1, icol] = fwindow[lh - tau + 1] * R
        tau = np.round(freq_bins / 2.0)
        if (ti <= signal.shape[0] - tau) and (ti >= tau + 1) and (tau <= lh):
            points = np.arange(-min([lg, signal.shape[0] - ti - tau]),
                               min([lg, ti - 1 - tau]) + 1)
            g2 = twindow[lg + 1 + points]
            g2 = g2 / np.sum(g2)
            _x = np.sum(g2 * signal[ti + tau - points] *
                        np.conj(signal[ti - tau - points]))
            _x *= fwindow[lh + tau + 1]
            _y = np.sum(g2 * signal[ti - tau - points] *
                        np.conj(signal[ti + tau - points]))
            _y *= fwindow[lh - tau + 1]
            tfr[tau, icol] = (_x + _y) * 0.5
    tfr = np.fft.fft(tfr, axis=0)
    return np.real(tfr)
logFile.write('nrOfVols=' + unicode(nrOfVols) + '\n')
logFile.write('durations=' + unicode(durations) + '\n')
logFile.write('totalTime=' + unicode(totalTime) + '\n')

# log opacity on/off cycle in ms
logFile.write('lenCycStim=' + unicode(cfg.lenCycStim) + '\n')
logFile.write('lenCycRamp=' + unicode(cfg.lenCycRamp) + '\n')
logFile.write('lenCycRest=' + unicode(cfg.lenCycRest) + '\n')
# derive how much of a second the stimlus, blank and ramp period should be
divStim = 1000 / cfg.lenCycStim
divRamp = 1000 / cfg.lenCycRamp
divRest = 1000 / cfg.lenCycRest

# define arrays to cycle opacity
cycAlt = np.hstack((
    signal.hamming(2 * cfg.nFrames / divRamp)[:cfg.nFrames / divRamp],
    np.ones(np.round(cfg.nFrames / divStim)),
    signal.hamming(2 * cfg.nFrames / divRamp)[cfg.nFrames / divRamp:],
    np.zeros(np.round(cfg.nFrames / divRest)),
    signal.hamming(2 * cfg.nFrames / divRamp)[:cfg.nFrames / divRamp],
    np.ones(np.round(cfg.nFrames / divStim)),
    signal.hamming(2 * cfg.nFrames / divRamp)[cfg.nFrames / divRamp:],
    np.zeros(np.round(cfg.nFrames / divRest)),
)).astype('float32')
cycTransp = np.zeros(2 * cfg.nFrames).astype('int8')

# set timing sequence for the texture
texTimeBlank = np.tile(
    np.repeat(np.array([0, 0]), cfg.nFrames / (cfg.cycPerSec * 2)),
    cfg.cycPerSec * 2).astype('int8')
texTimeFlicker = np.tile(
示例#52
0
        'font.family': 'serif',
        'text.usetex': True,
        'pgf.rcfonts': False,
    })

Fs = np.intc(802e3)  # receiver sample rate
dt = 1 / Fs
T = np.float(6e-3) / 4  # Pulse duration

# Time domain window for NLFM generation
NLFM = radar.chirp(Fs)
NLFM.fftLen = 2048

# Synthesize the target autocorrelation function
#window_t = signal.chebwin(np.intc(2048), 60)
window_t = signal.hamming(np.intc(2048))
#window_t = signal.gaussian(np.intc(2048), 360)
#window_t = signal.gaussian(np.intc(2048), 400)

NLFM.getCoefficients(window_t, targetBw=100e3 / 4, centerFreq=100e3 / 4, T=T)
sig_t = NLFM.genNumerical()
t = np.linspace(-T / 2, (T / 2) - dt, len(sig_t))
SNR = 10  # dB
SnrString = 'SNR_' + str(SNR)
sig_t = util.wgnSnr(sig_t, SNR)

########################################################################

# Generate linear chirp (simple)
T = 1e-6
T = T
示例#53
0
 def setUp(self):
     # directory where the test files are located
     self.path = os.path.join(os.path.dirname(__file__), 'data')
     file = os.path.join(self.path, '3cssan.hy.1.MBGA_Z')
     f = open(file)
     self.res = np.loadtxt(f)
     f.close()
     file = os.path.join(self.path, 'MBGA_Z.ASC')
     f = open(file)
     data = np.loadtxt(f)
     f.close()
     #self.path = os.path.dirname(__file__)
     #self.res = np.loadtxt("3cssan.hy.1.MBGA_Z")
     #data = np.loadtxt("MBGA_Z.ASC")
     self.n = 256
     self.fs = 75
     self.smoothie = 3
     self.fk = [2, 1, 0, -1, -2]
     self.inc = int(0.05 * self.fs)
     self.fc1 = 0.68
     self.nofb = 8
     #[0] Time (k*inc)
     #[1] A_norm
     #[2] dA_norm
     #[3] dAsum
     #[4] dA2sum
     #[5] ct
     #[6] dct
     #[7] omega
     #[8] domega
     #[9] sigma
     #[10] dsigma
     #[11] logcep
     #[12] logcep
     #[13] logcep
     #[14] dperiod
     #[15] ddperiod
     #[16] bwith
     #[17] dbwith
     #[18] cfreq
     #[19] dcfreq
     #[20] hob1
     #[21] hob2
     #[22] hob3
     #[23] hob4
     #[24] hob5
     #[25] hob6
     #[26] hob7
     #[27] hob8
     #[28] phi12
     #[29] dphi12
     #[30] phi13
     #[31] dphi13
     #[32] phi23
     #[33] dphi23
     #[34] lv_h1
     #[35] lv_h2
     #[36] lv_h3
     #[37] dlv_h1
     #[38] dlv_h2
     #[39] dlv_h3
     #[40] rect
     #[41] drect
     #[42] plan
     #[43] dplan
     self.data_win, self.nwin, self.no_win = \
         util.enframe(data, signal.hamming(self.n), self.inc)
示例#54
0
def hamming_data(d, window_length):
    '''Multiply all epochs with a hamming window.
	'''
    hamming = signal.hamming(window_length)
    return d * hamming
示例#55
0
    result2 = []
    for k in range(0, 100):
        for j in range(nw - k):
            sum = sum + Frame[i][j] * Frame[i][j + k]
        result.append(sum)
    #归一化
    for l in range(len(result)):
        result2.append(result[l] / result[0])
    return 8000 / result2.index(max(result2[-80:]))


filename = '01234.wav'
data = wavread(filename)
nw = 160  #窗大小
inc = 40  #窗偏移
winfunc = signal.hamming(nw)  #汉明窗
Frame, nf = enframe(data[0], nw, inc, winfunc)  #分帧加窗
Energy = calEnergy(Frame, nf, nw)  #计算每一帧的能量

#画出每一帧的能量图
x = np.arange(1, len(Energy) + 1)
plt.figure()
plt.title(u'每一帧的能量')
plt.xlabel(u'帧数')
plt.ylabel(u'能量')
plt.plot(x, Energy)
plt.show()

#计算浊音对应的帧
zhuo = calzhuo(Energy, nf)
print(zhuo)
示例#56
0
ax.set_ylabel("signal")
ax.legend()
fig.tight_layout()
fig.savefig("ch17-inverse-fft.pdf")
fig.savefig("ch17-inverse-fft.png")


# ## Windowing

# In[28]:

fig, ax = plt.subplots(1, 1, figsize=(8, 3))
N = 100
ax.plot(signal.blackman(N), label="Blackman")
ax.plot(signal.hann(N), label="Hann")
ax.plot(signal.hamming(N), label="Hamming")
ax.plot(signal.gaussian(N, N/5), label="Gaussian (std=N/5)")
ax.plot(signal.kaiser(N, 7), label="Kaiser (beta=7)")
ax.set_xlabel("n")
ax.legend(loc=0)
fig.tight_layout()
fig.savefig("ch17-window-functions.pdf")


# In[29]:

df = pd.read_csv('temperature_outdoor_2014.tsv', delimiter="\t", names=["time", "temperature"])


# In[30]:
示例#57
0
def animate(i):
    x = np.arange(len(data))
    X = data
    line.set_data(x, X)
    return line,


anim = animation.FuncAnimation(fig,
                               animate,
                               frames=1,
                               interval=100,
                               blit=False)

Q = queue.Queue()
sdr = RtlSdr()
window = signal.hamming(N)


def data_processing():
    global Q
    global data
    global window

    while 1:
        origin_data = Q.get()
        while not Q.empty():
            Q.get()
        window_data = origin_data
        ob_data = abs(fftpack.fft(window_data))
        #sdata=fftpack.fftshift(fftpack.fft(ob_data,overwrite_x=True))
        data = 20 * lg(ob_data / N)
x = np.transpose(np.array(x))

# Parametros del ruido normal
# El ruido tiene que estar 3 y 10db por abajo de la senoidal
u = 0  # Media
db = -30
v = (N / 2) * 10**(db / 10)  # Varianza

# Genero señal de ruido
n = np.sqrt(v) * np.random.randn(N, R) + u

# Le sumo el ruido a la señal senoidal
x = x + n

btWin = signal.hamming(N)
psdBT, fBT = blackmanTukey(x, btWin, N, fs)

plt.figure(1)
plt.plot(fBT, psdBT)
'''
# Obtengo la autocorrelacion de la señal
Sxx = np.correlate(x[:,0], x[:,0], mode='full')
#Sxx = np.correlate(x[:,0], x[:,0], mode='same')
Sxx = np.reshape(Sxx,(len(Sxx),1))

# Le aplico una ventana a la secuencia de autocorrelacion
w = np.reshape(bartlett(len(Sxx)),(len(Sxx),1))
SxxW = Sxx*w

# Obtengo el periodograma de la autocorrelacion
# vim:fenc=utf-8
#
# Copyright © 2015 jaidev <jaidev@newton>
#
# Distributed under terms of the MIT license.
"""
================================================
Time-frequency Resolution: Short Analysis Window
================================================

This example shows the effect of an analysis window which is short in time on
the time-frequency resolution. Specifically, smaller windows have good time
resolutions but poor frequency resolutions.

Figure 3.8 from the tutorial.
"""

import numpy as np
import matplotlib.pyplot as plt
from tftb.generators import atoms
from scipy.signal import hamming
from tftb.processing.linear import ShortTimeFourierTransform

coords = np.array([[45, .25, 32, 1], [85, .25, 32, 1]])
sig = atoms(128, coords)
x = np.real(sig)
window = hamming(17)
stft = ShortTimeFourierTransform(sig, n_fbins=128, fwindow=window)
stft.run()
stft.plot(show_tf=True, cmap=plt.cm.gray)
示例#60
0
def hamming2d(a, b):
    # build 2d window
    w2d = np.outer(ss.hamming(a), ss.hamming(b))
    return np.sqrt(w2d)