Matlab - 常用函数集锦

在使用matlab进行信号处理和图形绘制过程中,某些函数被频繁调用,所以有必要将这些常用函数进行总结归类。

滤波函数

低通滤波

function [filtered_signal,filtb,filta]=lopass_butterworth(inputsignal,cutoff_freq,Fs,order)
% Low-pass Butterworth filter
% [filtered_signal,filtb,filta] = lopass_butterworth(inputsignal,cutoff_freq,Fs,order)
%
% This is simply a set of built-in Matlab functions, repackaged for ease of
% use by Chad Greene, October 2012.
%
% INPUTS:
% inputsignal = input time series
% cutoff_freq = filter corner frequency
% Fs = data sampling frequency
% order = order of Butterworth filter
%
% OUTPUTS:
% filtered_signal = the filtered time series
% filtb, filta = filter numerator and denominator (optional)
%
% EXAMPLE 1:
% load train
% t = (1:length(y))/Fs;
% y_filt = lopass_butterworth(y,900,Fs,4); % cut off at 900 Hz
% figure
% plot(t,y,'b',t,y_filt,'r')
% xlabel('time in seconds')
% box off
% legend('unfiltered','filtered')
% sound(y,Fs)      % play original time series
% pause(2)         % pause two seconds
% sound(y_filt,Fs) % play filtered time series

nyquist_freq = Fs/2;  % Nyquist frequency
Wn=cutoff_freq/nyquist_freq;    % non-dimensional frequency
[filtb,filta]=butter(order,Wn,'low'); % construct the filter
filtered_signal=filtfilt(filtb,filta,inputsignal); % filter the data with zero phase

高通滤波

function [filtered_signal,filtb,filta]=hipass_butterworth(inputsignal,cutoff_freq,Fs,order)
% High-pass Butterworth filter
% [filtered_signal,filtb,filta] = hipass_butterworth(inputsignal,cutoff_freq,Fs,order)
%
% This is simply a set of built-in Matlab functions, repackaged for ease of
% use by Chad Greene, October 2012.
%
% INPUTS:
% inputsignal = input time series
% cutoff_freq = filter corner frequency
% Fs = data sampling frequency
% order = order of Butterworth filter
%
% OUTPUTS:
% filtered_signal = the filtered time series
% filtb, filta = filter numerator and denominator (optional)
%
% EXAMPLE 1:
% load train
% t = (1:length(y))/Fs;
% y_filt = hipass_butterworth(y,900,Fs,4); % cut off at 900 Hz
% figure
% plot(t,y,'b',t,y_filt,'r')
% xlabel('time in seconds')
% box off
% legend('unfiltered','filtered')
% sound(y,Fs)      % play original time series
% pause(2)         % pause two seconds
% sound(y_filt,Fs) % play filtered time series

nyquist_freq = Fs/2;  % Nyquist frequency
Wn=cutoff_freq/nyquist_freq;    % non-dimensional frequency
[filtb,filta]=butter(order,Wn,'high'); % construct the filter
filtered_signal=filtfilt(filtb,filta,inputsignal); % filter the data with zero phase

带通滤波

function [filtered_signal,filtb,filta]=bandpass_butterworth(inputsignal,cutoff_freqs,Fs,order)
% Bandpass Butterworth filter
% [filtered_signal,filtb,filta] = bandpass_butterworth(inputsignal,cutoff_freq,Fs,order)
%
% This is simply a set of built-in Matlab functions, repackaged for ease of
% use by Chad Greene, October 2012.
%
% INPUTS:
% inputsignal = input time series
% cutoff_freqs = filter corner frequencies in the form [f1 f2]
% Fs = data sampling frequency
% order = order of Butterworth filter
%
% OUTPUTS:
% filtered_signal = the filtered time series
% filtb, filta = filter numerator and denominator (optional)
%
% EXAMPLE 1:
% load train
% t = (1:length(y))/Fs;
% y_filt = bandpass_butterworth(y,[800 1000],Fs,4); % cut off below 800 Hz and above 1000 Hz
%
% figure
% plot(t,y,'b',t,y_filt,'r')
% xlabel('time in seconds')
% box off
% legend('unfiltered','filtered')
% sound(y,Fs)      % play original time series
% pause(2)         % pause two seconds
% sound(y_filt,Fs) % play filtered time series

nyquist_freq = Fs/2;  % Nyquist frequency
Wn=cutoff_freqs/nyquist_freq;    % non-dimensional frequency
[filtb,filta]=butter(order,Wn,'bandpass'); % construct the filter
filtered_signal=filtfilt(filtb,filta,inputsignal); % filter the data with zero phase

带阻滤波

function [filtered_signal,filtb,filta]=bandstop_butterworth(inputsignal,cutoff_freqs,Fs,order)
% Band-stop Butterworth filter
% [filtered_signal,filtb,filta] = bandstop_butterworth(inputsignal,cutoff_freqs,Fs,order)
%
% This is simply a set of built-in Matlab functions, repackaged for ease of
% use by Chad Greene, October 2012.
%
% INPUTS:
% inputsignal = input time series
% cutoff_freqs = filter corner frequencies in the form [f1 f2]
% Fs = data sampling frequency
% order = order of Butterworth filter
%
% OUTPUTS:
% filtered_signal = the filtered time series
% filtb, filta = filter numerator and denominator (option 大专栏  Matlab - 常用函数集锦al)
%
% EXAMPLE 1:
% load train
% t = (1:length(y))/Fs;
% y_filt = bandstop_butterworth(y,[800 1000],Fs,4); % cut off below 800 Hz and above 1000 Hz
%
% figure
% plot(t,y,'b',t,y_filt,'r')
% xlabel('time in seconds')
% box off
% legend('unfiltered','filtered')
% sound(y,Fs)      % play original time series
% pause(2)         % pause two seconds
% sound(y_filt,Fs) % play filtered time series

nyquist_freq = Fs/2;  % Nyquist frequency
Wn=cutoff_freqs/nyquist_freq;    % non-dimensional frequency
[filtb,filta]=butter(order,Wn,'stop'); % construct the filter
filtered_signal=filtfilt(filtb,filta,inputsignal); % filter the data with zero phase

绘图函数

function [  ] = setPlot( varargin )
% setPlot()
% setPlot(title)
% setPlot(title,xlabel)
% setPlot(title,xlable,ylabel)
% setPlot(title,xlabel,ylabel,xlim)
% setPlot(title,xlable,ylabel,xlim,ylim)

narginchk(0,5);     % 判断输入参数是否足够

grid on;
axis tight;

if nargin>=1
    title(varargin{1});
end

if nargin>=2
    xlabel(varargin{2});
end

if nargin>=3
    ylabel(varargin{3});
end

if nargin>=4
    xlim(varargin{4});
end

if nargin>=5
    ylim(varargin{5});
end

end

信号处理函数

频谱分析

function [freq,amp]=fft_signal(signal,fs,N)
% Spectrum analysis
% INPUTS:
% signal = input time series
% fs = data sampling frequency
% N = data length of signal
%
% OUTPUTS:
% freq = frequency of Spectrum
% amp = amplitude of Spectrum
%
% EXAMPLE 1:
% fs = 100;
% N = fs*10;
% t = (0:N-1)/fs;
% y = sin(2*pi*10*t);
% [freq,amp] = fft_signal(y,fs,N);
% plot(freq,amp);

amp = 2*abs(fft(signal))/N;        % 求取信号的幅度谱
amp = amp(1:fix(length(amp)/2));       % 截取有效部分
freq=(0:length(amp)-1)*fs/N;       % 横坐标代表频率
end

幅值分布

function [ amp,dist ] = ampDist( signal,sectionNum )
% Calculate the amplitude distribution of the signal
% INPUTS:
% signal : The signal to be analyzed
% sectionNum : Number of segments
%
% OUTPUTS:
% amp : Amplitude after segmentation
% dist :Amplitude distribution
%
% EXAMPLE 1:
% fs = 1000;
% N = fs*100;
% y = wgn(1,N,10);     % 高斯白噪声
% [amp,dist] = ampDist(y,500);
% bar(amp,dist);

yMin = min(signal);
yMax = max(signal);

amp = linspace(yMin,yMax,sectionNum);
dist = hist(signal,amp);
dist = dist./length(signal);
end

LMS最小均方算法

function [ y_error, y_filter ] = LMS( x_input,x_dest,M,u )
% LMS 最小均方算法
% INPUTS:
% x_input    原始信号
% x_dest     期望信号
% M          阶次
% u          步长因子
%
% OUTPUTS:
% y_error    误差信号
% y_filter   滤波器信号输出
%
% EXAMPLE 1:
% load train
% t = (1:length(y))/Fs;
% M = 2; u = 0.5;
% y_dest = (max(y)-min(y))/2*cos(2*pi*18*t);      % 参考信号
% [y_error,y_filter] = LMS(y,y_dest,M,u);
% plot(t,y_error,t,y_filter);

N = length(x_input);
y_filter = zeros(1,N);
y_error  = zeros(1,N);
h = zeros(1,M);

for k=M:N
    h_old = h;
    y_filter(k) = x_dest(k:-1:k-M+1)*h_old';
    y_error(k)  = x_input(k) - y_filter(k);
    h = h_old + 2*u*y_error(k)*x_dest(k:-1:k-M+1);
end

end

EMD经验模态分解

function imf = emd(x)
% Empiricial Mode Decomposition (Hilbert-Huang Transform)
% imf = emd(x)
% Funcs : ismonotonic, isimf, getspline, findpeaks

x   = transpose(x(:));      % 将x变为一维向量
imf = [];
while ~ismonotonic(x)
   x1 = x;
   sd = Inf;
   cnt=0;
   while (sd > 0.1) || ~isimf(x1)
      s1 = getspline(x1);
      s2 = -getspline(-x1);
      x2 = x1-(s1+s2)/2;

      sd = sum((x1-x2).^2)/sum(x1.^2);
      x1 = x2;
      cnt=cnt+1;
   end
%    cnt

   imf{end+1} = x1;
   x          = x-x1;
end
imf{end+1} = x;

% FUNCTIONS
% 判断信号的单调性
function u = ismonotonic(x)

u1 = length(findpeaks(x))*length(findpeaks(-x));
if u1 > 0
    u = 0;
else
    u = 1;
end

% 判断信号是否满足IMF条件
% 条件:极大值点数和极小值点数之和与过零点数相等或相差1?
function u = isimf(x)

N  = length(x);
u1 = sum(x(1:N-1).*x(2:N) < 0);
u2 = length(findpeaks(x))+length(findpeaks(-x));
if abs(u1-u2) > 1
    u = 0;
else
    u = 1;
end

% 使用三次样条函数,得到包络线
function s = getspline(x)

N = length(x);
p = findpeaks(x);
s = spline([0 p N+1],[0 x(p) 0],1:N);

% 寻找极大值点
function n = findpeaks(x)
% Find peaks.
% n = findpeaks(x)

n    = find(diff(diff(x) > 0) < 0);
u    = find(x(n+1) > x(n));
n(u) = n(u)+1;

原文地址:https://www.cnblogs.com/wangziqiang123/p/11712126.html

时间: 2024-10-11 17:33:22

Matlab - 常用函数集锦的相关文章

PHP盛宴——常用函数集锦

最近写了蛮多PHP,也接触到挺多常用的函数,大多都记了笔记,发个博客出来,共同学习.其实感觉学习一门语言,语法逻辑是软素质,而对语言的熟悉程度只能随着使用时间的增长而慢慢增长,当对一门语言的函数.库.特性都深深了解了,才能勉强称得上是熟练或者精通吧. 1. trim(),从字符串两端删除空白字符和其他预定义字符,当然可以删除指定的字符. 类似的还有ltrim().rtrim(). 2. __CLASS__,该常量返回该类被定义时的名字. 3. strtotime(),将任何英文文本的日期时间描述

SQL常用函数集锦

一.字符转换函数1.ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来,但含其它字符的字符串必须用‘’括起来使用,否则会出错. 2.CHAR()将ASCII 码转换为字符.如果没有输入0 ~ 255 之间的ASCII 码值,CHAR() 返回NULL . 3.LOWER()和UPPER()LOWER()将字符串全部转为小写:UPPER()将字符串全部转为大写. 4.STR()把数值型数据转换为字符型数据.STR (<float_exp

matlab 常用函数汇总

1. 特殊变量与常数 主题词 意义 主题词 意义 ans 计算结果的变量名 computer 确定运行的计算机 eps 浮点相对精度 Inf 无穷大 I 虚数单位 inputname 输入参数名 NaN 非数 nargin 输入参数个数 nargout 输出参数的数目 pi 圆周率 nargoutchk 有效的输出参数数目 realmax 最大正浮点数 realmin 最小正浮点数 varargin   实际输入的参量 varargout 实际返回的参量     2. 操作符与特殊字符 主题词

MATLAB常用函数(不定时更新)

1.pause 一般情况下pause(a)表示程序暂停a秒后继续执行,但有时候也存在这种情况,程序中只有pause:并没有参数a,这样的意思是程序暂停,按任意键程序继续执行.2.uiwait(h,timeout) uiwait(h,timeout) 阻止程序执行,直至调用了 uiresume.删除了图窗 h 或已经过 timeout 秒.timeout 的最小值为 1.如果 uiwait 收到一个更小的值,将发出警告并使用值为 1 秒的 timeout. 3.msgbox() h = msgbo

matlab常用函数

1)查找向量中某个元素的位置:a=[1,2,34,5],查找34的位置:ans=find(a==34) 2)统计某个元素出现的次数:ans.length就得到元素34出现的次数 3)norm(A)函数用于返回A的范数,如果A是向量(行向量.列向量),norm(A)就是计算A的2范数,等价于norm(A,2).二范数就是向量所有元素的平方和,再开二次方.(参考:http://blog.sina.com.cn/s/blog_7d36d1910100wh4x.html)

ob 缓冲区 常用函数集锦

ob_start();            //打开一个输出缓冲区,所有的输出信息不再直接发送到浏览器,而是保存在输出缓冲区里面. ob_clean();            //删除内部缓冲区的内容,不关闭缓冲区(不输出). ob_end_clean();        //删除内部缓冲区的内容,关闭缓冲区(不输出). ob_get_clean();        //返回内部缓冲区的内容,关闭缓冲区.相当于执行 ob_get_contents() and ob_end_clean() o

MATLAB 常用函数记录 (持续更新)

点滴1:importdata("data.txt") 将txt中的数据导入到MATLAB矩阵中. 点滴2:gcf为当前figure的句柄,gca为当前axes的句柄. 点滴3:MATLAB 绘图的基本就是set句柄.每个句柄含有多少属性通过get可以查看. 点滴4:复制一个矩阵,并组成新的矩阵,a=[1:255],b=repmat(a,x,y),b是以a矩阵为单位的x行y列的矩阵

jQuery的常用函数扩展

jQuery的开发常用函数集锦,欢迎大家交流学习 (function ($) { /**************************获得URL的参数************************************/ //参数:URL中的参数名 //返回值:该参数的值 $.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)&q

SQL1-(增删改查、常用函数)

USE flowershopdb --全球唯一标识符(GUID UUID) SELECT NEWID() --增删改查 --INSERT [INTO] <表名> [列名] VALUES <值列表> INSERT tb_user VALUES('haha','123') INSERT INTO tb_user VALUES('gege','123') INSERT INTO tb_user(u_name,u_pass) VALUES('wawa','123') --DELETE [F