def test_basic(self): assert_allclose(signal.barthann(6, sym=True), [0, 0.35857354213752, 0.8794264578624801, 0.8794264578624801, 0.3585735421375199, 0]) assert_allclose(signal.barthann(7), [0, 0.27, 0.73, 1.0, 0.73, 0.27, 0]) assert_allclose(signal.barthann(6, False), [0, 0.27, 0.73, 1.0, 0.73, 0.27])
def win_sel(win_str, win_size): """ Function returns a window vector based on window name. Note class can only use windows found in scipy.signal library. """ overlap = 0 if (win_str == 'blackmanharris'): win = sig.blackmanharris(win_size) overlap = .75 elif (win_str == 'blackman'): win = sig.blackman(win_size) elif (win_str == 'bartlett'): win = sig.bartlett(win_size) elif (win_str == 'hamming'): win = sig.hamming(win_size) elif (win_str == 'hanning'): win = sig.hanning(win_size) elif (win_str == 'hann'): win = sig.hann(win_size) elif (win_str == 'barthann'): win = sig.barthann(win_size) elif (win_str == 'triang'): win = sig.triang(win_size) elif (win_str == 'rect' or win_str == None): win = np.ones(win_size) else: print('Invalid Window Defined') return -1 return win, overlap
def f(): window_type = display['window_type'] window_size = int(display['size']) p = float(display['param']) window = np.zeros(window_size) if window_type == 'square': window = np.ones(window_size) elif window_type == 'exponential': window = np.exp(np.arange(window_size) * p) elif window_type == 'hanning': window = np.hanning(window_size) elif window_type == 'blackman': window = np.blackman(window_size) elif window_type == 'ricker': window = ricker(window_size, p) elif window_type == 'gaussian': window = gaussian(window_size, p) elif window_type == 'barthann': window = barthann(window_size) elif window_type == 'flattop': window = flattop(window_size) elif window_type == 'cosine': window = cosine(window_size) elif window_type == 'triangle': window = triang(window_size) return window
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
def recompute_window(self): zoom = int(self.width * (self.get_eff_sample_rate()) / self.zoom_fac) if self.filter == 'kaiser': self.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': self.window = signal.boxcar( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'hann': self.window = signal.hann( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'hamming': self.window = signal.hamming( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'blackman': self.window = signal.blackman( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'blackmanharris': self.window = signal.blackmanharris( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'bartlett': self.window = signal.bartlett( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'barthann': self.window = signal.barthann( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] elif self.filter == 'nuttall': self.window = signal.nuttall( freqshow.SDR_SAMPLE_SIZE, False, )[0:zoom + 2] else: self.window = 1
def getResult(): signal_1 = np.concatenate((np.sin(np.linspace(0, 50, 150)), np.zeros(80))) signal_2 = np.sin(np.linspace(0, 50, 150)) hann_window = signal.barthann(150) signal_with_hann_window = signal_2 * hann_window signal_with_hann_window = np.concatenate( (signal_with_hann_window, np.zeros(80))) spectr = np.fft.fft(signal_1) spectr_with_spectr_hann = np.fft.fft(signal_with_hann_window) return { 'signal_1': signal_1, 'spectr': spectr, 'changed_spectr': spectr_with_spectr_hann, 'signal_2': signal_with_hann_window }
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
(including the boxcar): """ fig02 = plt.figure() # Boxcar with zeroed out fraction b = sig.boxcar(npts) zfrac = 0.15 zi = int(npts*zfrac) b[:zi] = b[-zi:] = 0 name = 'Boxcar - zero fraction=%.2f' % zfrac winspect(b, fig02, name) winspect(sig.hanning(npts), fig02, 'Hanning') winspect(sig.bartlett(npts), fig02, 'Bartlett') winspect(sig.barthann(npts), fig02, 'Modified Bartlett-Hann') """ .. image:: fig/multi_taper_spectral_estimation_02.png As before, the left figure displays the windowing function in the temporal domain and the figure on the left displays the attentuation of spectral leakage in the other frequency bands in the spectrum. Notice that though different windowing functions have different spectral attenuation profiles, trading off attenuation of leakage from frequency bands near the frequency of interest (narrow-band leakage) with leakage from faraway frequency bands (broad-band leakage) they are all superior in both of these respects to the boxcar window used in the naive periodogram. Another approach which deals with both the inefficiency problem and with the
from scipy import signal from matplotlib import pyplot as plt from matplotlib import style import numpy as np from scipy.fftpack import fft, fftshift window = signal.barthann(51) plt.plot(window) plt.title("Barlett-Hann Window") plt.ylabel("Amplitude") plt.xlabel("Sample") plt.figure() A = fft(window, 2048) / (len(window) / 2.0) freq = np.linspace(-0.5, -0.5, len(A)) response = 20 * np.log10(np.abs(fftshift(A / abs(A).max()))) plt.plot(freq, response) plt.axis([-0.5, 0.5, -120, 0]) plt.title("Frequency response of the Barlett-Hann Window") plt.ylabel("Normalized magnitude(dB)") plt.xlabel("Noarmalized frequency (cyles/sample)") plt.show()
""" fig02 = plt.figure() # Boxcar with zeroed out fraction b = sig.boxcar(npts) zfrac = 0.15 zi = int(npts * zfrac) b[:zi] = b[-zi:] = 0 name = 'Boxcar - zero fraction=%.2f' % zfrac winspect(b, fig02, name) winspect(sig.hanning(npts), fig02, 'Hanning') winspect(sig.bartlett(npts), fig02, 'Bartlett') winspect(sig.barthann(npts), fig02, 'Modified Bartlett-Hann') """ .. image:: fig/multi_taper_spectral_estimation_02.png As before, the left figure displays the windowing function in the temporal domain and the figure on the left displays the attentuation of spectral leakage in the other frequency bands in the spectrum. Notice that though different windowing functions have different spectral attenuation profiles, trading off attenuation of leakage from frequency bands near the frequency of interest (narrow-band leakage) with leakage from faraway frequency bands (broad-band leakage) they are all superior in both of these respects to the boxcar window used in the naive periodogram. Another approach which deals with both the inefficiency problem and with the spectral leakage problem is the use of taper functions. In this approach, the
# Plot the window and its frequency response: from scipy import signal from scipy.fftpack import fft, fftshift import matplotlib.pyplot as plt window = signal.barthann(51) plt.plot(window) plt.title("Bartlett-Hann window") plt.ylabel("Amplitude") plt.xlabel("Sample") plt.figure() A = fft(window, 2048) / (len(window)/2.0) freq = np.linspace(-0.5, 0.5, len(A)) response = 20 * np.log10(np.abs(fftshift(A / abs(A).max()))) plt.plot(freq, response) plt.axis([-0.5, 0.5, -120, 0]) plt.title("Frequency response of the Bartlett-Hann window") plt.ylabel("Normalized magnitude [dB]") plt.xlabel("Normalized frequency [cycles per sample]")