故障诊断Matlab常用技巧

故障诊断Matlab常用技巧

1 巴特沃斯低通滤波+傅立叶变换

2 离散傅立叶变换

3 小波变换

4 短时傅立叶等等

具体做法见下处代码

clc
close all
%%for example
fs = 12800;
t = (0 :52121)/fs;
data = sin(t).*cos(t);
figure
FFT(data,fs)
figure
BFFT(data,fs)
figure
XFFT(data,fs)
figure
xiaobo(data(30000:40000),fs)

wlen = 512;
hop = 2;

figure
shortFouier(wlen,hop,data(30000:40000),fs);
%巴特沃斯低通滤波+傅里叶变换

function BFFT(data,fs)
    datay = data(30000:40000);  %取中间稳定数据
    lengthNormal = length(datay);
    datay = datay - mean(datay);
    wc = 300/(fs/2);        %低道滤波截止频率为300Hz
    [a,b] = butter(4,wc);
    datay = filter(a,b,datay);
    rifft = fft(datay);
    fz = (0:lengthNormal-1)*fs/lengthNormal;
    P2 = 2*abs(rifft/lengthNormal);
    
    plot(fz(1:lengthNormal/2),P2(1:lengthNormal/2),'r');
    ax = gca;
    ax.XLim = [0 300];
    
    xlabel('频率Hz');
    ylabel('幅值');
    title('频谱图(频域)');
end


%小波去噪
function data = loseNoise(data)
    lev = 15;
    data = wden(data,'heursure','s','one',lev,'db4');
end


%傅里叶
function FFT(data,fs)
    datay = data(30000:40000);  %取中间稳定数据
    lengthD = length(datay);
    rifft = fft(datay);
    fz = (0:lengthD - 1)*fs/lengthD;
    P2 = 2*abs(rifft)/lengthD;
    plot(fz(1:lengthD/2),P2(1:lengthD/2),'m');
    ax = gca;
    ax.XLim = [0 300];
    xlabel('频率(Hz)');
    ylabel('幅值');
    title('频谱图');
end


%小波去噪+傅里叶
function XFFT(data,fs)
    datay = loseNoise(data);
    FFT(datay,fs);
end
%短时傅里叶


function shortFouier(wlen,hop,data,fs)
    lengthData = length(data);
    Normal1 = wextend(1,'sym',data,round(lengthData/2));% 镜像延拓
    Normal2 = wkeep1(Normal1,lengthData + 1*wlen);
     % 短时 Fouier
    h = hamming(wlen);% 海明窗的窗长
    f = 0:1:200;
    [tfr2,f,t2] = spectrogram(Normal2,h,wlen-hop,f,fs);
    tfr2 = tfr2 * 2/wlen*2;
    imagesc(t2-wlen/fs/2,f,abs(tfr2));
    %[S,F,T,P] = spectrogram(Normal2,h,wlen-hop,f,frequency);
    %surf(T,F,10*log10(P),'edgecolor','none');
    %view(0,1000);
    ylim([-10 200])
    xlabel('Time, s');
    ylabel('Frequency, Hz');
    colorbar
end

%小波去噪+小波变换
function xiaobo(data,fs)
    data = loseNoise(data);
    t = (0:length(data)-1)/fs;
    plot(t,data);
    wavename = 'cmor3-3';
    totalscal = 1024;
    Fc = centfrq(wavename);
    c = 2*Fc*totalscal;
    scals = c./(1:totalscal);
    f = 8*scal2frq(scals,wavename,1/fs);
    coefs = cwt(data,scals,wavename);
    figure
    imagesc(t,f,abs(coefs));
    set(gca,'YDir','normal');
    colorbar;
    xlabel('时间 t/s');
    ylabel('频率 f/Hz');
    title('小波时频图');
end

你可能感兴趣的:(matlab,短时傅立叶变换,小波分析,傅立叶分析)