日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python實(shí)現(xiàn)低通濾波器代碼

瀏覽:226日期:2022-08-06 10:38:40

低通濾波器實(shí)驗(yàn)代碼,這是參考別人網(wǎng)上的代碼,所以自己也分享一下,共同進(jìn)步

# -*- coding: utf-8 -*-import numpy as npfrom scipy.signal import butter, lfilter, freqzimport matplotlib.pyplot as pltdef butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype=’low’, analog=False) return b, adef butter_lowpass_filter(data, cutoff, fs, order=5): b, a = butter_lowpass(cutoff, fs, order=order) y = lfilter(b, a, data) return y # Filter requirements.order = 6fs = 30.0 # sample rate, Hzcutoff = 3.667 # desired cutoff frequency of the filter, Hz # Get the filter coefficients so we can check its frequency response.b, a = butter_lowpass(cutoff, fs, order) # Plot the frequency response.w, h = freqz(b, a, worN=800)plt.subplot(2, 1, 1)plt.plot(0.5*fs*w/np.pi, np.abs(h), ’b’)plt.plot(cutoff, 0.5*np.sqrt(2), ’ko’)plt.axvline(cutoff, color=’k’)plt.xlim(0, 0.5*fs)plt.title('Lowpass Filter Frequency Response')plt.xlabel(’Frequency [Hz]’)plt.grid() # Demonstrate the use of the filter. # First make some data to be filtered.T = 5.0 # secondsn = int(T * fs) # total number of samplest = np.linspace(0, T, n, endpoint=False) # 'Noisy' data. We want to recover the 1.2 Hz signal from this.data = np.sin(1.2*2*np.pi*t) + 1.5*np.cos(9*2*np.pi*t) + 0.5*np.sin(12.0*2*np.pi*t) # Filter the data, and plot both the original and filtered signals.y = butter_lowpass_filter(data, cutoff, fs, order)plt.subplot(2, 1, 2)plt.plot(t, data, ’b-’, label=’data’)plt.plot(t, y, ’g-’, linewidth=2, label=’filtered data’)plt.xlabel(’Time [sec]’)plt.grid()plt.legend()plt.subplots_adjust(hspace=0.35)plt.show()

實(shí)際代碼,沒有整理,可以讀取txt文本文件,然后進(jìn)行低通濾波,并將濾波前后的波形和FFT變換都顯示出來

# -*- coding: utf-8 -*-import numpy as npfrom scipy.signal import butter, lfilter, freqzimport matplotlib.pyplot as pltimport osdef butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype=’low’, analog=False) return b, adef butter_lowpass_filter(data, cutoff, fs, order=5): b, a = butter_lowpass(cutoff, fs, order=order) y = lfilter(b, a, data) return y # Filter requirements.order = 5fs = 100000.0 # sample rate, Hzcutoff = 1000 # desired cutoff frequency of the filter, Hz # Get the filter coefficients so we can check its frequency response.# b, a = butter_lowpass(cutoff, fs, order) # Plot the frequency response.# w, h = freqz(b, a, worN=1000)# plt.subplot(3, 1, 1)# plt.plot(0.5*fs*w/np.pi, np.abs(h), ’b’)# plt.plot(cutoff, 0.5*np.sqrt(2), ’ko’)# plt.axvline(cutoff, color=’k’)# plt.xlim(0, 1000)# plt.title('Lowpass Filter Frequency Response')# plt.xlabel(’Frequency [Hz]’)# plt.grid() # Demonstrate the use of the filter. # First make some data to be filtered.# T = 5.0 # seconds# n = int(T * fs) # total number of samples# t = np.linspace(0, T, n, endpoint=False) # 'Noisy' data. We want to recover the 1.2 Hz signal from this.# # data = np.sin(1.2*2*np.pi*t) + 1.5*np.cos(9*2*np.pi*t) + 0.5*np.sin(12.0*2*np.pi*t) # Filter the data, and plot both the original and filtered signals.path = '*****'for file in os.listdir(path): if file.endswith('txt'): data=[] filePath = os.path.join(path, file) with open(filePath, ’r’) as f: lines = f.readlines()[8:] for line in lines: # print(line) data.append(float(line)*100) # print(len(data)) t1=[i*10 for i in range(len(data))] plt.subplot(231) # plt.plot(range(len(data)), data) plt.plot(t1, data, linewidth=2,label=’original data’) # plt.title(’ori wave’, fontsize=10, color=’#F08080’) plt.xlabel(’Time [us]’) plt.legend() # filter_data = data[30000:35000] # filter_data=data[60000:80000] # filter_data2=data[60000:80000] # filter_data = data[80000:100000] # filter_data = data[100000:120000] filter_data = data[120000:140000] filter_data2=filter_data t2=[i*10 for i in range(len(filter_data))] plt.subplot(232) plt.plot(t2, filter_data, linewidth=2,label=’cut off wave before filter’) plt.xlabel(’Time [us]’) plt.legend() # plt.title(’cut off wave’, fontsize=10, color=’#F08080’) # filter_data=zip(range(1,len(data),int(fs/len(data))),data) # print(filter_data) n1 = len(filter_data) Yamp1 = abs(np.fft.fft(filter_data) / (n1 / 2)) Yamp1 = Yamp1[range(len(Yamp1) // 2)] # x_axis=range(0,n//2,int(fs/len # 計(jì)算最大賦值點(diǎn)頻率 max1 = np.max(Yamp1) max1_index = np.where(Yamp1 == max1) if (len(max1_index[0]) == 2): print((max1_index[0][0] )* fs / n1, (max1_index[0][1]) * fs / n1) else: Y_second = Yamp1 Y_second = np.sort(Y_second) print(np.where(Yamp1 == max1)[0] * fs / n1, (np.where(Yamp1 == Y_second[-2])[0]) * fs / n1) N1 = len(Yamp1) # print(N1) x_axis1 = [i * fs / n1 for i in range(N1)] plt.subplot(233) plt.plot(x_axis1[:300], Yamp1[:300], linewidth=2,label=’FFT data’) plt.xlabel(’Frequence [Hz]’) # plt.title(’FFT’, fontsize=10, color=’#F08080’) plt.legend() # plt.savefig(filePath.replace('txt', 'png')) # plt.close() # plt.show() Y = butter_lowpass_filter(filter_data2, cutoff, fs, order) n3 = len(Y) t3 = [i * 10 for i in range(n3)] plt.subplot(235) plt.plot(t3, Y, linewidth=2, label=’cut off wave after filter’) plt.xlabel(’Time [us]’) plt.legend() Yamp2 = abs(np.fft.fft(Y) / (n3 / 2)) Yamp2 = Yamp2[range(len(Yamp2) // 2)] # x_axis = range(0, n // 2, int(fs / len(Yamp))) max2 = np.max(Yamp2) max2_index = np.where(Yamp2 == max2) if (len(max2_index[0]) == 2): print(max2, max2_index[0][0] * fs / n3, max2_index[0][1] * fs / n3) else: Y_second2 = Yamp2 Y_second2 = np.sort(Y_second2) print((np.where(Yamp2 == max2)[0]) * fs / n3, (np.where(Yamp2 == Y_second2[-2])[0]) * fs / n3) N2=len(Yamp2) # print(N2) x_axis2 = [i * fs / n3 for i in range(N2)] plt.subplot(236) plt.plot(x_axis2[:300], Yamp2[:300],linewidth=2, label=’FFT data after filter’) plt.xlabel(’Frequence [Hz]’) # plt.title(’FFT after low_filter’, fontsize=10, color=’#F08080’) plt.legend() # plt.show() plt.savefig(filePath.replace('txt', 'png')) plt.close() print(’*’*50) # plt.subplot(3, 1, 2) # plt.plot(range(len(data)), data, ’b-’, linewidth=2,label=’original data’) # plt.grid() # plt.legend() # # plt.subplot(3, 1, 3) # plt.plot(range(len(y)), y, ’g-’, linewidth=2, label=’filtered data’) # plt.xlabel(’Time’) # plt.grid() # plt.legend() # plt.subplots_adjust(hspace=0.35) # plt.show() ’’’ # Y_fft = Y[60000:80000] Y_fft = Y # Y_fft = Y[80000:100000] # Y_fft = Y[100000:120000] # Y_fft = Y[120000:140000] n = len(Y_fft) Yamp = np.fft.fft(Y_fft)/(n/2) Yamp = Yamp[range(len(Yamp)//2)] max = np.max(Yamp) # print(max, np.where(Yamp == max)) Y_second = Yamp Y_second=np.sort(Y_second) print(float(np.where(Yamp == max)[0])* fs / len(Yamp),float(np.where(Yamp==Y_second[-2])[0])* fs / len(Yamp)) # print(float(np.where(Yamp == max)[0]) * fs / len(Yamp)) ’’’

補(bǔ)充拓展:淺談opencv的理想低通濾波器和巴特沃斯低通濾波器

低通濾波器

1.理想的低通濾波器

python實(shí)現(xiàn)低通濾波器代碼

其中,D0表示通帶的半徑。D(u,v)的計(jì)算方式也就是兩點(diǎn)間的距離,很簡(jiǎn)單就能得到。

python實(shí)現(xiàn)低通濾波器代碼

使用低通濾波器所得到的結(jié)果如下所示。低通濾波器濾除了高頻成分,所以使得圖像模糊。由于理想低通濾波器的過度特性過于急峻,所以會(huì)產(chǎn)生了振鈴現(xiàn)象。

python實(shí)現(xiàn)低通濾波器代碼

2.巴特沃斯低通濾波器

python實(shí)現(xiàn)低通濾波器代碼

同樣的,D0表示通帶的半徑,n表示的是巴特沃斯濾波器的次數(shù)。隨著次數(shù)的增加,振鈴現(xiàn)象會(huì)越來越明顯。

python實(shí)現(xiàn)低通濾波器代碼

void ideal_Low_Pass_Filter(Mat src){Mat img;cvtColor(src, img, CV_BGR2GRAY);imshow('img',img);//調(diào)整圖像加速傅里葉變換int M = getOptimalDFTSize(img.rows);int N = getOptimalDFTSize(img.cols);Mat padded;copyMakeBorder(img, padded, 0, M - img.rows, 0, N - img.cols, BORDER_CONSTANT, Scalar::all(0));//記錄傅里葉變換的實(shí)部和虛部Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };Mat complexImg;merge(planes, 2, complexImg);//進(jìn)行傅里葉變換dft(complexImg, complexImg);//獲取圖像Mat mag = complexImg;mag = mag(Rect(0, 0, mag.cols & -2, mag.rows & -2));//這里為什么&上-2具體查看opencv文檔//其實(shí)是為了把行和列變成偶數(shù) -2的二進(jìn)制是11111111.......10 最后一位是0//獲取中心點(diǎn)坐標(biāo)int cx = mag.cols / 2;int cy = mag.rows / 2;//調(diào)整頻域Mat tmp;Mat q0(mag, Rect(0, 0, cx, cy));Mat q1(mag, Rect(cx, 0, cx, cy));Mat q2(mag, Rect(0, cy, cx, cy));Mat q3(mag, Rect(cx, cy, cx, cy)); q0.copyTo(tmp);q3.copyTo(q0);tmp.copyTo(q3); q1.copyTo(tmp);q2.copyTo(q1);tmp.copyTo(q2);//Do為自己設(shè)定的閥值具體看公式double D0 = 60;//處理按公式保留中心部分for (int y = 0; y < mag.rows; y++){double* data = mag.ptr<double>(y);for (int x = 0; x < mag.cols; x++){double d = sqrt(pow((y - cy),2) + pow((x - cx),2));if (d <= D0){}else{data[x] = 0;}}}//再調(diào)整頻域q0.copyTo(tmp);q3.copyTo(q0);tmp.copyTo(q3);q1.copyTo(tmp);q2.copyTo(q1);tmp.copyTo(q2);//逆變換Mat invDFT, invDFTcvt;idft(mag, invDFT, DFT_SCALE | DFT_REAL_OUTPUT); // Applying IDFTinvDFT.convertTo(invDFTcvt, CV_8U);imshow('理想低通濾波器', invDFTcvt);} void Butterworth_Low_Paass_Filter(Mat src){int n = 1;//表示巴特沃斯濾波器的次數(shù)//H = 1 / (1+(D/D0)^2n)Mat img;cvtColor(src, img, CV_BGR2GRAY);imshow('img', img);//調(diào)整圖像加速傅里葉變換int M = getOptimalDFTSize(img.rows);int N = getOptimalDFTSize(img.cols);Mat padded;copyMakeBorder(img, padded, 0, M - img.rows, 0, N - img.cols, BORDER_CONSTANT, Scalar::all(0)); Mat planes[] = { Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F) };Mat complexImg;merge(planes, 2, complexImg); dft(complexImg, complexImg); Mat mag = complexImg;mag = mag(Rect(0, 0, mag.cols & -2, mag.rows & -2)); int cx = mag.cols / 2;int cy = mag.rows / 2; Mat tmp;Mat q0(mag, Rect(0, 0, cx, cy));Mat q1(mag, Rect(cx, 0, cx, cy));Mat q2(mag, Rect(0, cy, cx, cy));Mat q3(mag, Rect(cx, cy, cx, cy)); q0.copyTo(tmp);q3.copyTo(q0);tmp.copyTo(q3); q1.copyTo(tmp);q2.copyTo(q1);tmp.copyTo(q2); double D0 = 100; for (int y = 0; y < mag.rows; y++){double* data = mag.ptr<double>(y);for (int x = 0; x < mag.cols; x++){//cout << data[x] << endl;double d = sqrt(pow((y - cy), 2) + pow((x - cx), 2));//cout << d << endl;double h = 1.0 / (1 + pow(d / D0, 2 * n));if (h <= 0.5){data[x] = 0;}else{//data[x] = data[x]*0.5;//cout << h << endl;}//cout << data[x] << endl;}}q0.copyTo(tmp);q3.copyTo(q0);tmp.copyTo(q3);q1.copyTo(tmp);q2.copyTo(q1);tmp.copyTo(q2);//逆變換Mat invDFT, invDFTcvt;idft(complexImg, invDFT, DFT_SCALE | DFT_REAL_OUTPUT); // Applying IDFTinvDFT.convertTo(invDFTcvt, CV_8U);imshow('巴特沃斯低通濾波器', invDFTcvt);}

以上這篇python實(shí)現(xiàn)低通濾波器代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美片第1页综合| 国产精品一区2区3区| 国产精品多人| 欧美激情福利| 国语精品一区| 亚洲美女久久精品| 欧美一级精品| 亚洲视频电影在线| 国产精品入口久久| 国产精品v亚洲精品v日韩精品| 精品国产乱码久久久| 欧美中文字幕一区二区| 99pao成人国产永久免费视频 | 成人日韩在线观看| 亚洲二区三区不卡| 日本欧美在线| 日本不卡免费高清视频在线| 欧美国产91| 婷婷综合成人| 国产成人在线中文字幕| 免费毛片在线不卡| 亚洲欧美高清| 麻豆视频观看网址久久| 婷婷成人在线| 欧美一级全黄| 亚洲精品一区三区三区在线观看| 久久午夜精品一区二区| 欧美日本二区| 日韩欧美另类一区二区| 亚洲免费资源| 日韩欧美一区二区三区在线观看 | 婷婷成人综合| 日本午夜精品| 亚洲一级黄色| 国产精品v日韩精品v欧美精品网站 | 欧美日韩18| 蜜桃av.网站在线观看| 亚洲无线观看| 色婷婷狠狠五月综合天色拍| 色综合视频一区二区三区日韩 | 欧美日韩水蜜桃| 久久激情五月婷婷| 激情久久久久久| 美女精品久久| 爽好多水快深点欧美视频| 国产中文欧美日韩在线| 亚洲精品裸体| 免费观看不卡av| 精品视频国产| 日韩精品导航| 日韩一级欧洲| 亚洲精品国产嫩草在线观看 | 日韩高清成人| 国产精品巨作av| 美女精品在线| 蜜臀国产一区| 久久麻豆视频| 日本国产一区| 老鸭窝亚洲一区二区三区| 亚洲www啪成人一区二区| 国产日产精品_国产精品毛片 | 激情综合自拍| 日本久久精品| 日韩精品成人| 免费日韩视频| 亚洲精品中文字幕乱码| 中文一区一区三区高中清不卡免费| 久久国产乱子精品免费女| 日韩精品一二三| 五月婷婷亚洲| 视频福利一区| 成人国产精品久久| 久久免费影院| 国产精品国产三级在线观看| 日韩中文av| 亚洲香蕉久久| 视频在线观看91| 国产免费成人| 国产在线成人| 亚洲大全视频| 国产精品毛片| 国产精品女主播一区二区三区| 亚洲精品网址| 黄色精品网站| 在线视频日韩| 日韩精品一区第一页| 石原莉奈一区二区三区在线观看| 欧美日韩国产免费观看视频| 91精品国产91久久久久久黑人| 日产午夜精品一线二线三线| 福利一区在线| 日韩中文首页| 99久久99视频只有精品| 中文字幕在线免费观看视频| 色综合狠狠操| 亚洲1234区| 婷婷久久一区| 一区二区电影| 国产日韩一区二区三免费高清| 欧美一区网站| 国产精品www994| 久久亚洲国产精品尤物| 国产成人在线中文字幕| 桃色av一区二区| 91精品综合| 国产精品丝袜xxxxxxx| 亚洲视频国产精品| 青青国产精品| 国产精品亚洲欧美一级在线| 久久久精品国产**网站| 黑人精品一区| 亚洲电影在线一区二区三区| 红桃视频国产一区| 亚洲网址在线观看| 欧美视频一区| av中文字幕在线观看第一页 | 日韩免费福利视频| 欧美成人综合| 亚洲视频国产| 鲁大师精品99久久久| 免费污视频在线一区| 水野朝阳av一区二区三区| 日精品一区二区三区| 精品欧美日韩精品| 在线视频观看日韩| 日韩中文av| 激情国产在线| 国产精品日韩| 国产精品videossex| 蜜桃精品在线| 亚洲精品在线a| 国产精品xx| 免费久久99精品国产| 免费一级欧美在线观看视频 | 中文在线中文资源| 999在线观看精品免费不卡网站| 91福利精品在线观看| av在线最新| 蜜桃视频一区二区三区在线观看| 欧美激情aⅴ一区二区三区| 亚洲天堂黄色| 国产精品一区二区av日韩在线| 99久久精品网| 欧美久久精品| 一区久久精品| 精品免费av| 午夜久久av| 久久婷婷丁香| 久久不见久久见免费视频7| 亚洲深夜福利| 精品视频在线观看网站| 亚洲三级毛片| 久久精品青草| 美腿丝袜亚洲一区| 在线看片一区| 久久久久国产| 国产精品v日韩精品v欧美精品网站 | 亚洲国产福利| 少妇精品久久久一区二区三区| 国产一二在线播放| 日韩成人一级| 一区二区视频欧美| 黄色aa久久| 国产精品一区二区三区av麻| 香蕉精品999视频一区二区| 日韩中文字幕高清在线观看| 国产欧美亚洲精品a| 免费人成网站在线观看欧美高清| 免费高潮视频95在线观看网站| 国产日韩一区二区三区在线| 免费看欧美美女黄的网站| 99精品综合| 久久久国产精品网站| 亚洲精品动态| 伊人久久大香线蕉av不卡| 动漫av一区| 国产精品免费精品自在线观看| 免费久久99精品国产自在现线| 欧美天堂视频| 成人精品国产亚洲| 国产精品久久免费视频| 亚洲精品综合| 免费日韩av片| 99国产精品| 91久久久精品国产| 久久久精品日韩| 欧产日产国产精品视频| 久久精品九色| 国产亚洲一区二区三区啪| 亚洲视频二区| 免费成人在线观看| 99国产精品私拍| 欧美精品一二| 亚洲高清影视| 午夜精品影院| 午夜久久99| 亚洲精品一二三区区别| 欧美日韩一区二区三区视频播放| 91综合视频| 日韩在线欧美|