python 小波_Matlab 小波功率谱&Python小波分析

602bf64077ee1ab53f4e99a2f215b0c3.png

%相关调用函数百度一下随处可见clc;clear;close alldata=load('tangyrtem1.txt');sst=data(:,2);variance = std(sst)^2;sst = (sst - mean(sst))/sqrt(variance) ;n = length(sst);dt = 1.0; %每个数据之间的时间步长。time = data(:,1);  % construct time array(构造时间数组)xlim = data(1,1)+[1,n-1];  % plotting range(绘图范围)pad = 1;      % pad the time series with zeroes (recommended)(用0填充时间序列(推荐))dj = 0.05;    % this will do 4 sub-octaves per octave; the spacing between discrete scales. Default is 0.25. a smaller # will give better scale resolution, but be slower to plot.(每个八度有四个亚八度;离散尺度之间的距离。默认是0.25。较小的#将提供更好的比例分辨率,但绘图较慢。)s0 = 2*dt;    % this says start at a scale of 6 months(这表示从6个月开始)j1 = 4/dj;    %%  Default is j1 = (LOG2(n DT/S0))/dj.  this says do 7 powers-of-two with dj sub-octaves each   zl:改变纵轴数值 64 128 256...(也就是说1是4,2是8,3是16,4是32,5是64,6是128)lag1 = 0.72;  % lag-1 autocorrelation for red noise backgroundmother = 'morlet';% Wavelet transform(小波变换):[wave,period,scale,coi] = wavelet(sst,dt,pad,dj,s0,j1,mother);power = (abs(wave)).^2 ;        % compute wavelet power spectrum(计算小波功率谱)% Significance levels: (variance=1 for the normalized SST)(显著性水平:(标准化海表温度方差=1))[signif,fft_theor] = wave_signif(1.0,dt,scale,0,lag1,0.90,-1,mother);sig95 = (signif')*(ones(1,n));  % expand signif --> (J+1)x(N) arraysig95 = power ./ sig95;         % where ratio > 1, power is significant% Global wavelet spectrum & significance levels(全局小波频谱及显著性水平):global_ws = variance*(sum(power')/n);   % time-average over all timesdof = n - scale;  % the -scale corrects for padding at edges(-scale修正了边的填充)global_signif = wave_signif(variance,dt,scale,1,lag1,0.90,dof,mother);% Scale-average between El Nino periods of 2--8 yearsavg = find((scale >=10 ) & (scale < 12));                          %zlCdelta = 0.776;   % this is for the MORLET waveletscale_avg = (scale')*(ones(1,n));  % expand scale --> (J+1)x(N) arrayscale_avg = power ./ scale_avg;   % [Eqn(24)]scale_avg = variance*dj*dt/Cdelta*sum(scale_avg(avg,:));   % [Eqn(24)]scaleavg_signif = wave_signif(variance,dt,scale,2,lag1,-1,[10,12],mother);%zl:检验whos%------------------------------------------------------ Plotting%--- Plot time seriessubplot('position',[0.08 0.72 0.65 0.2])          %0.1,0.75:起点;0.65:宽;0.2:高plot(time,sst)set(gca,'XLim',xlim(:))xlabel('Time (year)')ylabel('DegC')title('a) Tang Temperature Original Data')hold onp5 = polyfit(time,sst,1);         % 5 阶多项式拟合 y5 = polyval(p5,time);plot(time,y5,'-r');hold off%--- Contour plot wavelet power spectrumsubplot('position',[0.08 0.1 0.65 0.5])         % subplot('position',[0.15 0.10 0.5 0.6]) levels = [0.0625,0.125,0.25,0.5,1,2,4,8,16] ;Yticks = 2.^(fix(log2(min(period))):fix(log2(max(period))));imagesc(time,log2(period),log2(power));shading interpclim=get(gca,'clim'); %center color limits around log2(1)=0 围绕log2(1)=0的中心颜色限制    clim=[-1 1]*max(clim(2),3);    set(gca,'clim',clim)%contour(time,log2(period),log2(power),log2(levels),'LineWidth',3);  %*** or use 'contourf'% colorbar('position',[0.66 0.08 0.015 0.6],'LineWidth',3,'FontSize',30)%imagesc(time,log2(period),log2(power));  %*** uncomment for 'image' plotxlabel('Time (year)')ylabel('Period (year)')title('b) Wavelet Power Spectrum')set(gca,'XLim',xlim(:))set(gca,'YLim',log2([min(period),max(period)]), ...  'YDir','reverse', ...  'YTick',log2(Yticks(:)), ...  'YTickLabel',Yticks)% 95% significance contour, levels at -99 (fake) and 1 (95% signif)hold oncontour(time,log2(period),sig95,[-99,1],'k','linewidth',1.5);hold on% cone-of-influence, anything "below" is dubiousplot(time,log2(coi),'k')hold off% subplot('position',[left bottem width height])% 表示在当前图形的位置(position)上画图,% 该位置采用归一化的方式,% 即将当前的图形窗口左下角设置为[0,0],% 右上角设置为[1,1],[left bottom width height]中% left表示距离图形窗口左边的距离,% bottom表示距离窗口下边的距离,% width,heigth分别表示绘制坐标轴的大小,% 其中要注意的是left bottom width height这四个值都是0和1之间,刚才也说了,是归一化的坐标.%--- Plot global wavelet spectrumsubplot('position',[0.76 0.1 0.2 0.5])plot(global_ws,log2(period))hold onplot(global_signif,log2(period),'--')hold offxlabel('Power (mm)^2')title('c) Global Wavelet Spectrum')set(gca,'YLim',log2([min(period),max(period)]), ...  'YDir','reverse', ...  'YTick',log2(Yticks(:)), ...  'YTickLabel','')set(gca,'XLim',[0,1.25*max(global_ws)])

148876125d5a3849cdd63e8451d9ca43.png

3.2 小波分析Python

http://nicolasfauchereau.github.io/climatecode/posts/wavelet-analysis-in-python/

83806228e89af48aaf9b62891ee48ae2.png

b8ad505637b954bc03318d3895f87199.png

f2a69d4eefaa9aa2d3bfc26f13759542.png

4005f970fb962ca398284f060a49b152.png


4 Matlab colorbar

clc;clear;close all%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(1)surf(peaks);axis tightcolorbar;% colorbar('position',[0.95 0.1 0.04 0.8]);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(2);surf(peaks);axis tightcolorbar;caxis;caxis([0 3]); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(3)surf(peaks);axis tightcolorbar;caxis ;caxis([0 3]) ;cbarrow;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(4)surf(peaks); axis tightcolorbar('southoutside'); colormap(brewermap(256,'*RdBu'));caxis([-7 7]); cbarrow('right');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(5)surf(peaks); axis tightcolorbar('southoutside'); colormap(brewermap(256,'*RdBu'));caxis([-7 7]); cbarrow('left'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(51)surf(peaks); axis tightcolorbar('southoutside'); colormap(brewermap(256,'*RdBu'));caxis([-7 7]); cbarrow; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(6)surf(peaks); axis tightcolorbar('westoutside'); colormap(brewermap(256,'*RdBu'));caxis([-7 7]); cbarrow; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(7)surf(peaks); axis tightcolorbar('northoutside'); colormap(brewermap(256,'*RdBu'));caxis([-7 7]); cbarrow; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(8)surf(peaks); axis tightcolorbar('eastoutside'); colormap(brewermap(256,'*RdBu'));caxis([-7 7]); cbarrow;

5fbb38211377e5a65a67639ab8b81cb3.png

bcbbbf729d361add51930db1a2991d2e.png

bdd73c1e3d816a9de3f27a201dfaa5ae.png

d49aafc10c732d4378fb74ee9c7a9626.png

clc;clear;close alltick=-20:5:20;color=[69 117 180;116 173 203;171 217 233;254 224 144;253 174 77;244 109 67;215 48 39;165 0 38]/255;mode='h';colorbarn(tick,color,mode)
function [ax1,c] = colorbarn(tick,color,mode)%---------------------------------%带尖端的colorbar%[ax1,c] = COLORBARN(tick,color,mode)%坐标轴句柄是c,绘图窗句柄是ax1%详情请参考函数 [ax1,c,h]=contf_line(lon,lat,X,tick,color,mode)%---------------------------------[m,~]=size(color);len_tick=length(tick);if len_tick-m ~= 1    returnendfigure;set(gcf,'color','w');if isequal(mode,'h')    c=axes('position',[0.1 0.1 0.8 0.03]);    cdata=zeros(1,m);    for i=1:m        cdata(i)=(tick(i)+tick(i+1))/2;    end    axis([0 m 0 1])    line([0 1],[0 0],'linewidth',2,'color','w','parent',c)    line([m-1 m],[0 0],'linewidth',2,'color','w','parent',c)    colormap(color);    %------------------------    pat_v1=[1 0;1 1;2 1;2 0];pat_f1=reshape(1:(m-2)*4,4,m-2)';    for j=2:m-2        pat_v1=[pat_v1;[j 0;j 1;j+1 1;j+1 0]];    end    pat_col1=[cdata(2:end-1)]';    patch('Faces',pat_f1,'Vertices',pat_v1,'FaceVertexCData',pat_col1,'FaceColor','flat');    %--------------------------------------------------       pat_v2=[0 0.5;1 0;1 1;m-1 1;m-1 0;m 0.5];    pat_f2=[1 2 3;4 5 6];    pat_col2=[cdata(1);cdata(end)];    patch('Faces',pat_f2,'Vertices',pat_v2,'FaceVertexCData',pat_col2,'FaceColor','flat');    %---------------------------------------------------------    set(c,'color','none','xcolor','k','ycolor','none');    box off%     line([1 m-1],[0 0],'color','k')%     line([1 m-1],[1 1],'color','k')%     for i=2:m-2%         line([i i],[0 1],'color','k')%     end    set(c,'xtick',1:m-1,'xticklabel',num2cell(tick(2:end-1)),'ytick',[])    ax1=axes('position',[0.1 0.2 0.8 0.7]);elseif isequal(mode,'v')    c=axes('position',[0.87 0.11 0.02 0.81]);    set(c,'yaxislocation','right');    cdata=zeros(1,m);    for i=1:m        cdata(i)=(tick(i)+tick(i+1))/2;    end    axis([0 1 0 m])    line([1 1],[0 1],'linewidth',2,'color','w','parent',c)    line([1 1],[m-1 m],'linewidth',2,'color','w','parent',c)    colormap(color);    %--------------------------------------------    pat_v1=[0 1;1 1;1 2;0 2];pat_f1=reshape(1:(m-2)*4,4,m-2)';    for j=2:m-2        pat_v1=[pat_v1;[0 j;1 j;1 j+1;0 j+1]];    end    pat_col1=[cdata(2:end-1)]';    patch('Faces',pat_f1,'Vertices',pat_v1,'FaceVertexCData',pat_col1,'FaceColor','flat');    %--------------------------------------------------       pat_v2=[0.5 0;1 1;0 1;1 m-1;0.5 m;0 m-1];    pat_f2=[1 2 3;4 5 6];    pat_col2=[cdata(1);cdata(end)];    patch('Faces',pat_f2,'Vertices',pat_v2,'FaceVertexCData',pat_col2,'FaceColor','flat');    %------------------------------------------------------    set(c,'color','none','xcolor','none','ycolor','k');    box off    set(c,'ytick',1:m-1,'yticklabel',num2cell(tick(2:end-1)),'xtick',[])    ax1=axes('position',[0.11 0.11 0.74 0.81]);else    disp('colorbarn格式出错')    returnendend

6d8faf3a115cbc56e0e4c07588c07f1c.png

clc;clear;close allcolor=[131 0 162;160 0 198;109 0 219;31 60 249;0  160 230;0 198 198;0 209 139;0 219 0;160 230  51;230 219 51;230 175 45;239 130 41;239 0 0;219  0 98;255 1 118]/255;tick=515:5:590;mode='v';%----------------------------------------------------------data=importdata('fill_china.dat');lon=data(:,1);lat=data(:,2);data_9=[6 6];%lon lat data_9是绘图数据[ax1,c,h]=contf_line(lon,lat,data_9,tick,color,mode);set(gca,'fontname','Times New  Roman','fontsize',16,'fontangle','italic');set(c,'fontname','Times New Roman','fontsize',14)
function [ax1,c,h]=contf_line(lon,lat,X,tick,color,mode)% ---------------------------------%contourf()绘图,带尖端的colorbar%[ax1,c,h]=CONTF_LINE(lon,lat,X,tick,color,mode)% %tick的个数比color的行数多一个%ax1控制绘图窗,c控制colorbar,h控制contourf函数%--------%修改colorbar间隔:%lab=get(c,'x/yticklabel');%lab(2:2:end)={' '};%set(c,'x/yticklabel',lab);%--------%copyright 傅辉煌% ----------------------------------------[m,~]=size(color);len_tick=length(tick);if len_tick-m ~= 1    returnend[ax1,c]=colorbarn(tick,color,mode);[~,h]=contourf(lon,lat,X);set(h,'levellist',tick,'linecolor','none');colormap(color);caxis([tick(1) tick(end)])end

b0484415d6f98cae4675a2610b69ba40.png

% 计算两个经纬度之间的距离% m_map工具箱里面的m_lldist% DIST=m_lldist([20 30],[44 45])clear all;close all;clcload('data.mat')load('map.mat');space_shp=0.05;xi=round(min(x_gz))-1:space_shp:round(max(x_gz))+1;yi=round(min(y_gz))-1:space_shp:round(max(y_gz))+1;[XI,YI]=meshgrid(xi,yi);ZI1=interp2(lon,lat,data,XI,YI,'nearest');    % 'linear'、'nearest'、'natural'、'cubic' 或 'v4'。默认方法为 'linear'isin=inpolygon(XI,YI,x_gz,y_gz);ZI1(~isin)=NaN;%% 降水量不等间距填色图% %降水量颜色设置cmap=[255 255 255; 150 255 150; 50 146 60; 100 184 244;...          0 0 241; 255 0 255; 192 0 100;192 100 100;]/255;level = [0.1,0.1,10,25,50,100,200,250] ;[~,ZI2]=histc(ZI1,level);ZI2=ZI2-1;%% 绘图figure('position',[50 50 800 600]);m_proj('mercator','lon',[103,110],'lat',[24,30]);[cs,h2]=m_contourf(XI,YI,ZI2,'LineWidth',1.5);  hold on  % --------------------------set(h2,'LineStyle','none');shading flat;           %填充平滑colormap(cmap) ;caxis([0 length(level)]) ;cbar = colorbar;set(cbar,'Ticks',0:length(level)-1,'TickLabels',level) ;set(cbar,'TickLength',0);m_plot(x_gz2,y_gz2,'LineWidth',1.5,'Color','k');set(gca,'FontSize',13);%m_grid('linest','none','tickdir','in');m_grid('box','fancy','tickdir','in');xlabel('经度');ylabel('纬度');title1=strcat('24h降水量');title(title1,'FontSize',13);% print('-dpng',strcat(title1,'.png'));hold off

a65d0482e9b603d6c9dd0768ef16dc8c.png

f910242d846c2051c2a233d67aff85af.png

5 Plan

文章链接:Plan4

e6a4d8f96f68f30433a86b05ecd9d2d9.png8b74026b49e5e972bf3aada6acd3eecc.png114b9a9f114aadcd282379036e59088e.png1e0278863cc666bdc81137cd914336fd.png

4bf8586eb7d915547affc2d4567a7e79.png

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python小波分析是一种用于时间序列分析的方法,它可以对信号进行频域分析和时域分解。小波分析基于相似性原理,通过比较原信号与小波基函数的相似性来计算小波系数。这些系数反映了原信号与每个小波基函数在不同尺度上的相似程度。 在Python中进行小波分析可以使用Matplotlib库中的add_subplot和subplots_adjust方法来绘制小波分解图和功率谱图。add_subplot方法用于创建子图,subplots_adjust方法用于调整子图之间的间距。这些方法可以帮助我们更好地可视化小波分析的结果。 同时,默认的小波基函数为morlet小波,在小波分析中起到了重要的作用。通过对信号进行小波分解和小波重构,我们可以得到信号的频域信息和时域分解结果。这样可以帮助我们更好地理解和分析时间序列数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于python进行小波分析,频率谱分析](https://blog.csdn.net/qq_32832803/article/details/111866444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python连续小波分析CWT](https://blog.csdn.net/weixin_46713695/article/details/127234673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值