小波与傅里叶分析基础_浅析傅里叶频谱分析&滤波&小波分析

该博客介绍了如何使用MATLAB进行傅里叶频谱分析和小波分析,通过用户交互选框来指定信号的分析区间。原始信号包含20Hz、50Hz和100Hz的正弦波。文章提供了实现带通滤波的代码,并展示了时频图,以揭示不同频率成分的发生时刻。
摘要由CSDN通过智能技术生成

78332ea80fd756736aa2aa9d8965f6bf.png

原始信号由20Hz、50Hz和100Hz三种频率的正弦波组成,如上图所示。

在Figure图上画框截取待分析的数据,如下所示:

873e1eed818aa765d96e2be8defa250c.png

所得结果如下所示,

4b4342133ed072524b0848f4bcc2ba50.png

d68e6d8920b7db10a0ac49dc39c949cf.png

从上图中可以清晰的看出有三种频率,而且从时频图中可以看到三种频率发生在不 同时刻。

点击单边傅里叶频谱,放大频谱如下:

eb6504c0b34cb63915bba489a9e94416.png

7aee8519e820d5d573b623aa2884ba9d.png

画框可以选定滤波范围,如带通滤波,选择如下框,可得结果:

3b4ea1981a4e8f44d96f7a96f9e05c82.png

1b3e6b943a3d05375094a97f5254bf14.png

MATLAB中具体代码如下:

  1. function frequency_analysis

  2. clc close all

  3. Ts = 0.001;

  4. Fs = 1/Ts;

  5. f1 = 20;

  6. f2 = 50;

  7. f3 = 100;

  8. dt = 0.2;

  9. t1 = (0:Ts:dt-Ts) + 0; t2 = (0:Ts:dt-Ts) + dt;

  10. t3 = (0:Ts:dt-Ts) + 2*dt;

  11. y1 = sin(2*pi*f1*t1);

  12. y2 = sin(2*pi*f2*t2);

  13. y3 = sin(2*pi*f3*t3);

  14. t = [t1 t2 t3];

  15. y = [y1 y2 y3];

  16. figure

  17. plot(t,y)

  18. xlim([t(1) t(end)])

  19. ylim([min(y) max(y)])

  20. xlabel('时间t')

  21. ylabel('信号y(t)')

  22. title('原始信号')

  23. %

  24. % 以下为标准化程序

  25. % 上面的Ts和Fs要给定,后面会用到

  26. set(gcf,'WindowButtonDownFcn',@BtndownFcn);

  27. function BtndownFcn(h,evt)

  28. temppt = get(gca,'CurrentPoint');

  29. startpt.x = temppt(1,1);

  30. startpt.y = temppt(1,2);

  31. endpt = startpt;

  32. height = 0.0001;

  33. width = 0.0001;

  34. rectangle('Position',[startpt.x, startpt.y, width, height],'Tag','rangerect'); set(h,'WindowButtonMotionFcn',@BtnmoveFcn);

  35. set(h,'WindowButtonUpFcn',@BtnupFcn);

  36. function BtnmoveFcn(h,evt)

  37. temppt = get(gca,'CurrentPoint');

  38. endpt.x = temppt(1,1);

  39. endpt.y = temppt(1,2);

  40. width = abs(endpt.x-startpt.x)+0.00001;

  41. height = abs(endpt.y-startpt.y)+0.00001;

  42. hrect = findobj('Tag','rangerect');

  43. set(hrect,'Position',[min(startpt.x, endpt.x), min(startpt.y, endpt.y), width, height]);

  44. end

  45. function BtnupFcn(h,evt)

  46. set(h,'WindowButtonMotionFcn','');

  47. set(h,'WindowButtonUpFcn','');

  48. hrect = findobj('Tag','rangerect');

  49. delete(hrect);

  50. BtnUp_Spectrum_Analysis(startpt.x, endpt.x)

  51. end

  52. % 频谱分析

  53. function BtnUp_Spectrum_Analysis(starttime, endtime)

  54. hline = findobj(gca,'type','line');

  55. time = get(hline,'xdata');

  56. laser = get(hline,'ydata');

  57. % 得到截取的分析数据

  58. tempx = find(time >= min(starttime, endtime));

  59. startindex = tempx(1);

  60. tempx = find(time <= max(starttime, endtime));

  61. endindex = tempx(end);

  62. yt = laser(startindex:endindex);

  63. yt = ytmean(yt);

  64. t = time(startindex:endindex);

  65. % 傅里叶变换

  66. [Yf, f] = Spectrum_Calc(yt,Fs);

  67. % 小波变换

  68. scale = 1:50;

  69. cw2 = cwt(yt,scale,'morl');

  70. % 作图

  71. figure

  72. subplot(231)

  73. % 截取的频谱分析的数据

  74. plot(t, yt)

  75. xlim([t(1) t(end)])

  76. ylim([min(yt),max(yt)]);

  77. title('频谱分析数据')

  78. xlabel('时间t')

  79. ylabel('截取的数据y(t)')

  80. h1 = subplot(234);

  81. % 单边傅里叶变换分析频谱

  82. plot(f,Yf,'-')

  83. title('单边傅里叶频谱')

  84. xlabel('频率Hz')

  85. ylabel('|Y(f)|')

  86. set(h1,'ButtonDownFcn',@BtnDown_Filter_fcn)

  87. function BtnDown_Filter_fcn(h,evt)

  88. fig_fft = figure;

  89. plot(f,Yf,'-')

  90. title('单边傅里叶频谱')

  91. xlabel('频率Hz')

  92. ylabel('|Y(f)|')

  93. xlim([0 Fs/2])

  94. ylim([0 max(Yf)])

  95. % 构造右键菜单

  96. filter_flag = 1;

  97. hcmenu = uicontextmenu;

  98. uimenu(hcmenu, 'Label', '低通滤波', 'Callback', @hcb1);

  99. uimenu(hcmenu, 'Label', '高通滤波', 'Callback', @hcb2);

  100. uimenu(hcmenu, 'Label', '带通滤波', 'Callback', @hcb3);

  101. uimenu(hcmenu, 'Label', '带阻滤波', 'Callback', @hcb4);

  102. set(gca,'UIContextMenu',hcmenu);

  103. function hcb1(h,evt)

  104. filter_flag = 1;

  105. end

  106. function hcb2(h,evt)

  107. filter_flag = 2;

  108. end

  109. function hcb3(h,evt)

  110. filter_flag = 3;

  111. end

  112. function hcb4(h,evt)

  113. filter_flag = 4;

  114. end

  115. set(fig_fft,'WindowButtonDownFcn',@BtndownFcn);

  116. function BtndownFcn(h,evt)

  117. if

  118. strcmp(get(h,'SelectionType'),'normal')

  119. temppt = get(gca,'CurrentPoint');

  120. startpt.x = temppt(1,1);

  121. startpt.y = temppt(1,2);

  122. endpt = startpt;

  123. height = 0.0001;

  124. width = 0.0001;

  125. rectangle('Position',

  126. [startpt.x, startpt.y, width, height],'Tag','rangerect_fft'); set(h,'WindowButtonMotionFcn',@BtnmoveFcn);

  127. set(h,'WindowButtonUpFcn',@BtnupFcn);

  128. end

  129. function BtnmoveFcn(h,evt)

  130. temppt = get(gca,'CurrentPoint');

  131. endpt.x = temppt(1,1);

  132. endpt.y = temppt(1,2);

  133. width = abs(endpt.xstartpt.x)+0.00001;

  134. height = abs(endpt.ystartpt.y)+0.00001;

  135. hrect = findobj(h,'Tag','rangerect_fft');

  136. set(hrect,'Position', [min(startpt.x, endpt.x), min(startpt.y, endpt.y), width, height]);

  137. end

  138. function BtnupFcn(h,evt)

  139. set(h,'WindowButtonMotionFcn','');

  140. set(h,'WindowButtonUpFcn','');

  141. hrect = findobj(h,'Tag','rangerect_fft');

  142. delete(hrect);

  143. Filter_Analysis(startpt.x, endpt.x);

  144. function Filter_Analysis(x1,x2)

  145. lowfre = min(x1,x2);

  146. highfre = max(x1,x2);

  147. if

  148. lowfre <= 0 || lowfre >= Fs/2

  149. lowfre = 0.01;

  150. end

  151. if

  152. highfre <= 0 || highfre >= Fs/2

  153. highfre= Fs/2-0.01;

  154. end

  155. W1 = lowfre/(Fs/2);

  156. W2 = highfre/(Fs/2);

  157. filter_str = '(低通)';

  158. switch filter_flag

  159. case 1

  160. [b,a] = butter(5,min(W1,W2)); % 低通,画的框的左边为截止频率

  161. filter_str = '(低通)';

  162. case 2

  163. [b,a] = butter(5,max(W1,W2),'high'); % 高通,画的框的右边为截止频率

  164. filter_str = '(高通)';

  165. case 3

  166. [b,a] = butter(5,[W1 W2]); % 带通

  167. filter_str = '(带通)';

  168. case 4

  169. [b,a] = butter(5,[W1 W2],'stop'); % 带阻

  170. filter_str = '(带阻)';

  171. end

  172. y = filter(b,a,yt);

  173. figure

  174. subplot(2,1,1)

  175. plot(t,yt)

  176. xlabel('时间t')

  177. ylabel('信号y')

  178. xlim([t(1) t(end)])

  179. ylim([min(yt),max(yt)]);

  180. title('原始信 号')

  181. subplot(2,1,2)

  182. plot(t,y)

  183. xlabel('时间t')

  184. ylabel('信号y')

  185. xlim([t(1) t(end)])

  186. ylim([min(y),max(y)]);

  187. title(sprintf('滤波信号%s%.2fHz~%.2fHz',filter_str,lowfre,highfre))

  188. end

  189. end

  190. end

  191. end

  192. subplot(1,3,[2,3]) % 频率轴化为频率

  193. [X,Y] = meshgrid(t,5/(2*pi)./scale*Fs);

  194. mesh(X,Y,abs(cw2))

  195. view(0,90)

  196. title('时频图')

  197. xlabel('时间')

  198. ylabel('频率')

  199. xlim([t(1) t(end)])

  200. set(gca,'ylim',[0,max(max(Y))])

  201. set(gca,'YScale','log')

  202. set(gca,'YTick', [1:9,10:10:90,100:100:900,1000,2000])

  203. function [Yf, f] = Spectrum_Calc(yt,Fs)

  204. L = length(yt);

  205. NFFT = 2^nextpow2(L);

  206. Yf = fft(yt,NFFT)/L;

  207. Yf = 2*abs(Yf(1:NFFT/2+1));

  208. f = Fs/2*linspace(0,1,NFFT/2+1);

  209. end

  210. end

  211. end

  212. end

  213. 其中选框的代码可以标准化,可以用来在用户交互中用户选择范围,代码整理如下

  214. set(gcf,'WindowButtonDownFcn',@BtndownFcn);

  215. function BtndownFcn(h,evt)

  216. temppt = get(gca,'CurrentPoint');

  217. startpt.x = temppt(1,1);

  218. startpt.y = temppt(1,2);

  219. endpt = startpt;

  220. height = 0.0001;

  221. width = 0.0001;

  222. rectangle('Position',[startpt.x, startpt.y, width, height],'Tag','rangerect'); set(h,'WindowButtonMotionFcn',@BtnmoveFcn);

  223. set(h,'WindowButtonUpFcn',@BtnupFcn);

  224. function BtnmoveFcn(h,evt)

  225. temppt = get(gca,'CurrentPoint');

  226. endpt.x = temppt(1,1);

  227. endpt.y = temppt(1,2);

  228. width = abs(endpt.x-startpt.x)+0.00001;

  229. height = abs(endpt.y-startpt.y)+0.00001;

  230. hrect = findobj('Tag','rangerect');

  231. set(hrect,'Position',[min(startpt.x, endpt.x), min(startpt.y, endpt.y), width, height]);

  232. end

  233. function BtnupFcn(h,evt)

  234. set(h,'WindowButtonMotionFcn','');

  235. set(h,'WindowButtonUpFcn','');

  236. hrect = findobj('Tag','rangerect');

  237. delete(hrect);

  238. % ProsessFcn(startpt,endpt);

  239. % 选完框后对选择的 范围处理

  240. end

  241. end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值