def test_cheb_even_low_attenuation(self): cheb_even_low_at_true = array([1.000000, 0.451924, 0.51027, 0.541338, 0.541338, 0.51027, 0.451924, 1.000000]) with suppress_warnings() as sup: sup.filter(UserWarning, "This window is not suitable") cheb_even = windows.chebwin(8, at=-10) assert_array_almost_equal(cheb_even, cheb_even_low_at_true, decimal=4)
def test_cheb_odd_low_attenuation(self): cheb_odd_low_at_true = array([1.000000, 0.519052, 0.586405, 0.610151, 0.586405, 0.519052, 1.000000]) with suppress_warnings() as sup: sup.filter(UserWarning, "This window is not suitable") cheb_odd = windows.chebwin(7, at=10) assert_array_almost_equal(cheb_odd, cheb_odd_low_at_true, decimal=4)
def test_basic(self): with suppress_warnings() as sup: sup.filter(UserWarning, "This window is not suitable") assert_allclose(windows.chebwin(6, 100), [0.1046401879356917, 0.5075781475823447, 1.0, 1.0, 0.5075781475823447, 0.1046401879356917]) assert_allclose(windows.chebwin(7, 100), [0.05650405062850233, 0.316608530648474, 0.7601208123539079, 1.0, 0.7601208123539079, 0.316608530648474, 0.05650405062850233]) assert_allclose(windows.chebwin(6, 10), [1.0, 0.6071201674458373, 0.6808391469897297, 0.6808391469897297, 0.6071201674458373, 1.0]) assert_allclose(windows.chebwin(7, 10), [1.0, 0.5190521247588651, 0.5864059018130382, 0.6101519801307441, 0.5864059018130382, 0.5190521247588651, 1.0]) assert_allclose(windows.chebwin(6, 10, False), [1.0, 0.5190521247588651, 0.5864059018130382, 0.6101519801307441, 0.5864059018130382, 0.5190521247588651])
def array_factor(number_of_elements, scan_angle, element_spacing, frequency, theta, window_type, side_lobe_level): """ Calculate the array factor for a linear binomial excited array. :param window_type: The string name of the window. :param side_lobe_level: The sidelobe level for Tschebyscheff window (dB). :param number_of_elements: The number of elements in the array. :param scan_angle: The angle to which the main beam is scanned (rad). :param element_spacing: The distance between elements. :param frequency: The operating frequency (Hz). :param theta: The angle at which to evaluate the array factor (rad). :return: The array factor as a function of angle. """ # Calculate the wavenumber k = 2.0 * pi * frequency / c # Calculate the phase psi = k * element_spacing * (cos(theta) - cos(scan_angle)) # Calculate the coefficients if window_type == 'Uniform': coefficients = ones(number_of_elements) elif window_type == 'Binomial': coefficients = binom(number_of_elements - 1, range(0, number_of_elements)) elif window_type == 'Tschebyscheff': warnings.simplefilter("ignore", UserWarning) coefficients = chebwin(number_of_elements, at=side_lobe_level, sym=True) elif window_type == 'Kaiser': coefficients = kaiser(number_of_elements, 6, True) elif window_type == 'Blackman-Harris': coefficients = blackmanharris(number_of_elements, True) elif window_type == 'Hanning': coefficients = hanning(number_of_elements, True) elif window_type == 'Hamming': coefficients = hamming(number_of_elements, True) # Calculate the offset for even/odd offset = int(floor(number_of_elements / 2)) # Odd case if number_of_elements & 1: coefficients = roll(coefficients, offset + 1) coefficients[0] *= 0.5 af = sum(coefficients[i] * cos(i * psi) for i in range(offset + 1)) return af / amax(abs(af)) # Even case else: coefficients = roll(coefficients, offset) af = sum(coefficients[i] * cos((i + 0.5) * psi) for i in range(offset)) return af / amax(abs(af))
def test_cheb_even_high_attenuation(self): with suppress_warnings() as sup: sup.filter(UserWarning, "This window is not suitable") cheb_even = windows.chebwin(54, at=40) assert_array_almost_equal(cheb_even, cheb_even_true, decimal=4)
#Problema 8 import plotly.graph_objects as go from numpy import * from filter_lib_711 import * from palitos2 import stem_plot import scipy.signal.windows as ssw ws = 0.6 * pi #freq de rechazo sup. wp = 0.7 * pi #freq de paso sup. a = 60 #atenuacion deseada delta_w = abs(wp - ws) #banda de transicion. delta_f = delta_w / (2 * pi) #banda de transicion normalizada #Orden del filtro M = int(ceil(a / (22 * delta_f))) n = arange(M) wc = (ws + wp) / 2 #freq de corte superior. h = fpb_ideal(pi, M) - fpb_ideal(wc, M) #dif de sin cardinales #h = -fpb_ideal(wc,M) #diferencia de sin cardinal w = ssw.chebwin(M, a) #ventana chebyshev hn = h * w all_plots(hn=hn, wn=w, fs=100000000, store='eps')
w1 = array([1] * M) z = [0] * (N - M) wz = array(list(w1) + z) r, i, m1, a = mi_dft(wz) w2 = blackman(M) z = [0] * (N - M) wz = array(list(w2) + z) r, i, m2, a = mi_dft(wz) w3 = kaiser(M, 4) z = [0] * (N - M) wz = array(list(w3) + z) r, i, m3, a = mi_dft(wz) w4 = ssw.chebwin(M, -50) z = [0] * (N - M) wz = array(list(w4) + z) r, i, m4, a = mi_dft(wz) fig = go.Figure() fig.add_trace(go.Scatter(x=arange(M), y=m1, name='Rectangular')) fig.add_trace(go.Scatter(x=arange(M), y=m2, name='Blackman')) fig.add_trace(go.Scatter(x=arange(M), y=m3, name='Kaiser')) fig.add_trace(go.Scatter(x=arange(M), y=m4, name='Chebyshev')) fig.update_xaxes(title_text='n') fig.update_yaxes(title_text='W(m)') fig.update_layout(title='Window Sample', xaxis=dict(range=[0, M - 1])) fig.write_image("P6/W(m).eps") m1n = 20 * log10(m1 / m1[0])
#Cuarto import plotly.graph_objects as go from numpy import * from filter_lib_711 import * from palitos2 import stem_plot import plotly.express as px import scipy.signal.windows as ssw from psutil import * #from pandas import * #step frequencies wp = 0.4 * pi #frequencia de rechazo ws = 0.5 * pi #atenucion deseada a = 60 #banda de transicion normalizada delta_f = (ws - wp) * (2 * pi) #orden del filtro M = int(ceil(a / (22 * delta_f))) n = arange(M) #frecuencia de corte (filtro pasa bajas ideal) wc = (ws + wp) / 2 hd = fpb_ideal(wc, M) w_cheb = ssw.chebwin(M, a) h = hd * w_cheb all_plots(hn=h, wn=w_cheb, fs=100000000, store='eps')
import numpy as np import matplotlib.pyplot as plt from scipy.signal import hilbert, chirp, convolve import scipy.io.wavfile as wav from scipy.signal.windows import chebwin rate, data = wav.read("test01.wav") analytic_signal = hilbert(data) amplitude_env = np.abs(analytic_signal) win = chebwin(441,at=1,sym=False) conv_signal = convolve(data,data,mode='same') # plt.plot(data[(20*np.int(rate*0.01)):25*(np.int(rate*0.01))],label='signal') plt.plot(conv_signal,label='env') plt.show()
def pre_processing(self): """ Complete various pre-processing steps for encoded protein sequences before doing any of the DSP-related functions or transformations. Zero-pad the sequences, remove any +/- infinity or NAN values, get the approximate protein spectra and window function parameter names. Parameters ---------- :self (PyDSP object): instance of PyDSP class. Returns ------- None """ #zero-pad encoded sequences so they are all the same length self.protein_seqs = zero_padding(self.protein_seqs) #get shape parameters of proteins seqs self.num_seqs = self.protein_seqs.shape[0] self.signal_len = self.protein_seqs.shape[1] #replace any positive or negative infinity or NAN values with 0 self.protein_seqs[self.protein_seqs == -np.inf] = 0 self.protein_seqs[self.protein_seqs == np.inf] = 0 self.protein_seqs[self.protein_seqs == np.nan] = 0 #replace any NAN's with 0's #self.protein_seqs.fillna(0, inplace=True) self.protein_seqs = np.nan_to_num(self.protein_seqs) #initialise zeros array to store all protein spectra self.fft_power = np.zeros((self.num_seqs, self.signal_len)) self.fft_real = np.zeros((self.num_seqs, self.signal_len)) self.fft_imag = np.zeros((self.num_seqs, self.signal_len)) self.fft_abs = np.zeros((self.num_seqs, self.signal_len)) #list of accepted spectra, window functions and filters all_spectra = ['power', 'absolute', 'real', 'imaginary'] all_windows = [ 'hamming', 'blackman', 'blackmanharris', 'gaussian', 'bartlett', 'kaiser', 'barthann', 'bohman', 'chebwin', 'cosine', 'exponential' 'flattop', 'hann', 'boxcar', 'hanning', 'nuttall', 'parzen', 'triang', 'tukey' ] all_filters = [ 'savgol', 'medfilt', 'symiirorder1', 'lfilter', 'hilbert' ] #set required input parameters, raise error if spectrum is none if self.spectrum == None: raise ValueError( 'Invalid input Spectrum type ({}) not available in valid spectra: {}' .format(self.spectrum, all_spectra)) else: #get closest correct spectra from user input, if no close match then raise error spectra_matches = (get_close_matches(self.spectrum, all_spectra, cutoff=0.4)) if spectra_matches == []: raise ValueError( 'Invalid input Spectrum type ({}) not available in valid spectra: {}' .format(self.spectrum, all_spectra)) else: self.spectra = spectra_matches[0] #closest match in array if self.window_type == None: self.window = 1 #window = 1 is the same as applying no window else: #get closest correct window function from user input window_matches = (get_close_matches(self.window, all_windows, cutoff=0.4)) #check if sym=True or sym=False #get window function specified by window input parameter, if no match then window = 1 if window_matches != []: if window_matches[0] == 'hamming': self.window = hamming(self.signal_len, sym=True) self.window_type = "hamming" elif window_matches[0] == "blackman": self.window = blackman(self.signal_len, sym=True) self.window = "blackman" elif window_matches[0] == "blackmanharris": self.window = blackmanharris(self.signal_len, sym=True) #** self.window_type = "blackmanharris" elif window_matches[0] == "bartlett": self.window = bartlett(self.signal_len, sym=True) self.window_type = "bartlett" elif window_matches[0] == "gaussian": self.window = gaussian(self.signal_len, std=7, sym=True) self.window_type = "gaussian" elif window_matches[0] == "kaiser": self.window = kaiser(self.signal_len, beta=14, sym=True) self.window_type = "kaiser" elif window_matches[0] == "hanning": self.window = hanning(self.signal_len, sym=True) self.window_type = "hanning" elif window_matches[0] == "barthann": self.window = barthann(self.signal_len, sym=True) self.window_type = "barthann" elif window_matches[0] == "bohman": self.window = bohman(self.signal_len, sym=True) self.window_type = "bohman" elif window_matches[0] == "chebwin": self.window = chebwin(self.signal_len, sym=True) self.window_type = "chebwin" elif window_matches[0] == "cosine": self.window = cosine(self.signal_len, sym=True) self.window_type = "cosine" elif window_matches[0] == "exponential": self.window = exponential(self.signal_len, sym=True) self.window_type = "exponential" elif window_matches[0] == "flattop": self.window = flattop(self.signal_len, sym=True) self.window_type = "flattop" elif window_matches[0] == "boxcar": self.window = boxcar(self.signal_len, sym=True) self.window_type = "boxcar" elif window_matches[0] == "nuttall": self.window = nuttall(self.signal_len, sym=True) self.window_type = "nuttall" elif window_matches[0] == "parzen": self.window = parzen(self.signal_len, sym=True) self.window_type = "parzen" elif window_matches[0] == "triang": self.window = triang(self.signal_len, sym=True) self.window_type = "triang" elif window_matches[0] == "tukey": self.window = tukey(self.signal_len, sym=True) self.window_type = "tukey" else: self.window = 1 #window = 1 is the same as applying no window #calculate convolution from protein sequences if self.convolution is not None: if self.window is not None: self.convoled_seqs = signal.convolve( self.protein_seqs, self.window, mode='same') / sum( self.window) if self.filter != None: #get closest correct filter from user input filter_matches = (get_close_matches(self.filter, all_filters, cutoff=0.4)) #set filter attribute according to approximate user input if filter_matches != []: if filter_matches[0] == 'savgol': self.filter = savgol_filter(self.signal_len, self.signal_len) elif filter_matches[0] == 'medfilt': self.filter = medfilt(self.signal_len) elif filter_matches[0] == 'symiirorder1': self.filter = symiirorder1(self.signal_len, c0=1, z1=1) elif filter_matches[0] == 'lfilter': self.filter = lfilter(self.signal_len) elif filter_matches[0] == 'hilbert': self.filter = hilbert(self.signal_len) else: self.filter = "" #no filter
import plotly.graph_objects as go from dft_lib_correct import * from numpy import * import scipy.signal.windows as ssw N = 512 M = 32 w1 = array([1] * M) z = [0] * (N - M) wz = array(list(w1) + z) r, i, m1, a = mi_dft(wz) w2 = ssw.chebwin(M, -20 * 1.5) z = [0] * (N - M) wz = array(list(w2) + z) r, i, m2, a = mi_dft(wz) w3 = ssw.chebwin(M, -20 * 2) z = [0] * (N - M) wz = array(list(w3) + z) r, i, m3, a = mi_dft(wz) w4 = ssw.chebwin(M, -20 * 2.5) z = [0] * (N - M) wz = array(list(w4) + z) r, i, m4, a = mi_dft(wz) w5 = ssw.chebwin(M, -20 * 3) z = [0] * (N - M) wz = array(list(w5) + z)
#problema 9 import plotly.graph_objects as go from numpy import * from filter_lib_711 import * from palitos2 import stem_plot import scipy.signal.windows as ssw wp1 = 0.2*pi ws1 = 0.3*pi ws2 = 0.7*pi wp2 = 0.8*pi a = 60 delta_w = min(abs(ws1-wp1),abs(wp2-ws2)) #seleccion de la banda de trans. delta_f = delta_w/(2*pi)# banda de transicion normalizada #orden del filtro M = int(ceil(a/(22*delta_f))) n = arange(M) wc1 = (wp1*ws1)/2 wc2 = (ws2*wp2)/2 h = fpb_ideal(pi,M) - fpb_ideal(wc2,M) + fpb_ideal(wc1,M) w = ssw.chebwin(M,a) hn = h*w all_plots(hn=hn,wn=w,fs = 100000000, store='eps')