Matlab图片处理函数小记

来源:互联网 发布:mmd走路动作数据下载 编辑:程序博客网 时间:2024/04/30 18:17

上一篇博客还是去年4月写的,一年来自己还算有所成长。

本文总结一下自己写论文时候用的图片处理函数和命令。陆续缓慢更新

常用命令

坐标轴设置

坐标轴刻度设定
ylim([-1600 1600])set(gca,'ytick',[-1600:500:1600])

坐标轴标记设定

x=[150 300 450 600 750 900 1050 1200 1350 1500 ];set(gca,'XTickLabel',x)


常用函数

紧凑画图

function ha = tight_subplot (Nh, Nw, gap, marg_h, marg_w)%TIGHT_SUBPLOT suplot子图间距调整,自适应调整间距、空白% 代码参考: http://blog.csdn.net/lanchunhui/article/details/49820721% % 输入:行数Nh,列数Nw 用法同subplot(row,col)%        gap     归一化坐标轴间间距 (0...1)%                or [gap_h gap_w] 垂直、水平 for different gaps in height and width %        marg_h  归一化上下边距 (0...1)%                or [lower upper] for different lower and upper margins %        marg_w  归一化左右边距 (0...1)%                or [left right] for different left and right margins % 输出:ha  子图的句柄数组%                   starting from upper left corner, going row-wise as in%                   going row-wise as in%%  Example: ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])%           for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end%           set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')if nargin<3; gap = .02; endif nargin<4 || isempty(marg_h); marg_h = .05; endif nargin<5; marg_w = .05; endif numel(gap)==1     gap = [gap gap];endif numel(marg_w)==1     marg_w = [marg_w marg_w];endif numel(marg_h)==1     marg_h = [marg_h marg_h];endaxh = (1-sum(marg_h)-(Nh-1)*gap(1))/Nh; axw = (1-sum(marg_w)-(Nw-1)*gap(2))/Nw;py = 1-marg_h(2)-axh; k=0;ha = zeros(Nh*Nw,1);for ih = 1:Nh    px = marg_w(1);    for ix = 1:Nw        k = k+1;        ha(k) = axes('Units','normalized','Position',[px py axw axh], ...            'XTickLabel','', 'YTickLabel','');        px = px+axw+gap(2);    end    py = py-axh-gap(1);end

由fig仿真/实验图分析电流THD


%BYN_FFTBOX Compute Current THD, based on Matlab FFT Analysis Tool, code by Byn 170502% 该脚本用于计算fig图中的电流THD,仿真、实验图均可,ver 1.0% 调用Matlab自带的FFT分析函数,会计算分数次谐波,计算结果与Simulink FFT分析的结果完全一致% 可按照需求枚举基频(通常异步电机需要这个功能),可选从fig名中自动读取基频% 可选择同时显示转矩、电流、THD在一张图中% 目前代码中还有些冗余,可继续优化,未来可添加转矩纹波,转矩峰峰值等功能% 在Matlab R2016b 环境下编写%% 参数设置figname='d50.fig';ia_id=4; % 电流在fig中的通道号te_id=2; % 转矩在fig中的通道号WL_flag=0; % 是否采用枚举法确定基频,0不使用,1使用first=50; last=51; delta=0.1; % 起始频率,结束频率,枚举步长cycle=2;  % 计算周期XLabelStyle=2; %x 1: harmonic order; 2: frequency (Hz)start=0; % 从波形的什么时刻开始计算Te_plot=0; % 0:仅绘制电流及THD, 1:同时绘制转矩波形maxFrequency=20e3;  % 最大绘图显示频率(均计算到Nyquest频率)% tmp=regexp(figname,'\d*\.?\d*','match'); first=str2double(tmp{1,1}); % 从文件名自动读取基频%% 读入fig图clear lh n gcfopen(figname);lh=findall(gcf,'type','line');n=numel(lh);if n==0    error('获取图片信息失败!检查待处理图片是否在工作目录。');endx=get(lh(n-ia_id+1),'xdata')'; % 时间y=zeros(numel(x),2);y(:,1)=get(lh(n-te_id+1),'ydata')'; % 转矩y(:,2)=get(lh(n-ia_id+1),'ydata')'; % 电流%% 构造示波器结构体myscope.time=x;myscope.blockName='UserBuildFromOutData';myscope.signals.values=y(:,2);myscope.signals.dimensions=1;myscope.signals.label=[];myscope.signals.title=[];myscope.signals.plotStyle=0;%% 求取THD 单纯求取 或 枚举最小值后求取if WL_flag == 0    FFTDATA=Matlab_FFT (myscope,start,first,maxFrequency,cycle); %仅包含电流的示波器结构体,起始计算时间,基波,最大频率,周期数else    freq_fund=1e6; min_thd=1e6;    for f=first:delta:last        FFTDATA=Matlab_FFT (myscope,start,f,maxFrequency,cycle);        if FFTDATA.THD<min_thd            min_thd=FFTDATA.THD;            freq_fund=f;        end    end    FFTDATA=Matlab_FFT (myscope,start,freq_fund,20e3,cycle);    disp(['基频 ',num2str(freq_fund),' Hz THD = ',num2str(min_thd),'%'])end%% 绘图gcf=figure('NumberTitle', 'off', 'Name',[figname(1:end-4),' THD']);set(gcf,'PaperSize',[680,480],'color','w');cut=[FFTDATA.startTime FFTDATA.startTime+1.0/FFTDATA.fundamental*FFTDATA.cycles];  %截取区间tmp=find(x>=cut(1) & x<=cut(2));x=x(tmp)-cut(1);y=y(tmp,:);if Te_plot==1    ha = tight_subplot(3,1,[.1 .3],[.11 .06],[.1 .03]);    axes(ha(1)); plot(x,y(:,1), 'k');ylabel('T_e(Nm)');  ylim([-2 2]); grid on    xlabel('t/s');    set(ha(1),'XTick',0:1/FFTDATA.fundamental/2:2/FFTDATA.fundamental,'XLim',[0,2/FFTDATA.fundamental])    myTHDplot(ha(2:3),FFTDATA,x,y(:,2),XLabelStyle)else    ha = tight_subplot(2,1,[.15 .3],[.11 .06],[.1 .03]);    myTHDplot(ha,FFTDATA,x,y(:,2),XLabelStyle)    axes(ha(1));xlabel('t/s');endclear gcf lh ha myscope FFTDATAclear cut cycle delta figname first freq_fund ha ia_id lastclear n start te_id Te_plot tmp WL_flag x y XLabelStyle maxFrequency%% 绘制原始波形和THD频谱function myTHDplot(ha,FFTDATA,x,y,XLabelStyle)% 输入 坐标轴句柄,FFT结构体,时间坐标,电流数据,x轴标注类型freq_fund=FFTDATA.fundamental;freq1=FFTDATA.freq;mag1=FFTDATA.mag;v1=FFTDATA.magFundamental;nb_cycles=FFTDATA.cycles;% nfmax=FFTDATA.maxFrequency;order=floor(FFTDATA.freq(end)/FFTDATA.fundamental);% subplot(211)  %  画原始波形axes(ha(1));plot(x,y,'k');ylabel('i_{a}/A');grid on%xlabel('t/s');% ylabel('U_{ab}/V')set(gca,'XTick',0:1/freq_fund/2:nb_cycles/freq_fund,'XLim',[0,nb_cycles/freq_fund])axis([0 nb_cycles/freq_fund min(y)*1.2 max(y)*1.2])% subplot(212)  %  画频谱axes(ha(2));if XLabelStyle==1  % Harmonic order    bar(freq1/freq_fund,mag1/v1*100,'k')    xlabel('Harmonic order'),    ylabel('H_{n}/H_{1} (%)'),    axis([0 order 0 max(mag(nb_cycles+2:end))/v1*120])  % 显示范围为幅值最高的谐波else % display frequency (Hz) at xlabel    bar(freq1,mag1/v1*100,'k')    xlabel('Frequency (kHz)'),    ylabel('H_{n}/H_{1} (%)'),    axis([0 max(freq1) 0 max(mag1(nb_cycles+2:end))/v1*120])     set(ha(2),'XTickLabel',0:max(freq1)/1000/10:max(freq1)/1000); % 将标度由Hz转换为kHz%     clear gcfendq=['Fundamental ( ',num2str(freq_fund),'Hz ) = ',num2str(v1),', THD = ',num2str(FFTDATA.THD),'%'];% q=['THD = ',num2str(FFTDATA.THD),'%'];title(q)disp(q)end%% Matlab FFT Analysis Tool% reference http://cn.mathworks.com/help/physmod/sps/powersys/ref/power_fftscope.html;jsessionid=db0b9a8ee73742bf537d1f0a3291?refresh=truefunction FFTDATA=Matlab_FFT (myscope,start,fundamental,maxFrequency,cycle)% 输入:仅包含电流的示波器结构体,起始计算时间,基波,最大频率,周期数% 输出 FFT结构体FFTDATA = power_fftscope(myscope);FFTDATA.startTime=start;FFTDATA.cycles=cycle;FFTDATA.fundamental=fundamental;FFTDATA.maxFrequency=maxFrequency+1; % 这里+1不会影响计算结果,只是为了画图时横轴坐标更好处理FFTDATA=power_fftscope(FFTDATA);% power_fftscope(FFTDATA)endfunction ha = tight_subplot (Nh, Nw, gap, marg_h, marg_w)%TIGHT_SUBPLOT suplot子图间距调整,自适应调整间距、空白% code by Byn 161110 ver 1.0% 代码参考: http://blog.csdn.net/lanchunhui/article/details/49820721% % 输入:行数Nh,列数Nw 用法同subplot(row,col)%        gap     归一化坐标轴间间距 (0...1)%                or [gap_h gap_w] 垂直、水平 for different gaps in height and width %        marg_h  归一化上下边距 (0...1)%                or [lower upper] for different lower and upper margins %        marg_w  归一化左右边距 (0...1)%                or [left right] for different left and right margins % 输出:ha  子图的句柄数组%                   starting from upper left corner, going row-wise as in%                   going row-wise as in%%  Example: ha = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])%           for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end%           set(ha(1:4),'XTickLabel',''); set(ha,'YTickLabel','')if nargin<3; gap = .02; endif nargin<4 || isempty(marg_h); marg_h = .05; endif nargin<5; marg_w = .05; endif numel(gap)==1     gap = [gap gap];endif numel(marg_w)==1     marg_w = [marg_w marg_w];endif numel(marg_h)==1     marg_h = [marg_h marg_h];endaxh = (1-sum(marg_h)-(Nh-1)*gap(1))/Nh; axw = (1-sum(marg_w)-(Nw-1)*gap(2))/Nw;py = 1-marg_h(2)-axh; k=0;ha = zeros(Nh*Nw,1);for ih = 1:Nh    px = marg_w(1);    for ix = 1:Nw        k = k+1;        ha(k) = axes('Units','normalized','Position',[px py axw axh], ...            'XTickLabel','', 'YTickLabel','');        px = px+axw+gap(2);    end    py = py-axh-gap(1);endend


0 0
原创粉丝点击