您的位置:首页 > 编程语言 > MATLAB

matlab相关,来自一个初学者的收藏

2020-03-06 13:35 816 查看

从网上看到的,值得收藏一下。
在使用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 = fs10;
% t = (0:N-1)/fs;
% y = sin(2pi10t);
% [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 = fs100;
% 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))/2cos(2pi18t); % 参考信号
% [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?k-M+1)h_old’;
y_error(k) = x_input(k) - y_filter(k);
h = h_old + 2u*y_error(k)*x_dest(k?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§ 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;

e后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用

TOC
语法后生成一个完美的目录。

如何改变文本的样式

强调文本 强调文本

加粗文本 加粗文本

标记文本

删除文本

引用文本

H2O is是液体。

210 运算结果是 1024.

插入链接与图片

链接: link.

图片:

带尺寸的图片:

居中的图片:

居中并且带尺寸的图片:

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的

代码片
.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

  • 项目 项目 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

创建一个表格

一个简单的表格是这么创建的:

项目 Value
电脑 $1600
手机 $12
导管 $1

设定内容居中、居左、居右

使用

:---------:
居中
使用
:----------
居左
使用
----------:
居右

第一列 第二列 第三列
第一列文本居中 第二列文本居右 第三列文本居左

SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPE ASCII HTML
Single backticks
'Isn't this fun?'
‘Isn’t this fun?’
Quotes
"Isn't this fun?"
“Isn’t this fun?”
Dashes
-- is en-dash, --- is em-dash
– is en-dash, — is em-dash

创建一个自定义列表

Markdown
Text-to-HTML conversion tool
Authors
John
Luke

如何创建一个注脚

一个具有注脚的文本。1

注释也是必不可少的

Markdown将文本转换为 HTML。

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ(n)=(n−1)!∀n∈N\Gamma(n) = (n-1)!\quad\forall n\in\mathbb NΓ(n)=(n−1)!∀n∈N 是通过欧拉积分

Γ(z)=∫0∞tz−1e−tdt . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=∫0∞​tz−1e−tdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

Mon 06Mon 13Mon 20已完成 进行中 计划一 计划二 现有任务Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:

张三李四王五你好!李四, 最近怎么样?你最近怎么样,王五?我很好,谢谢!我很好,谢谢!李四想了很长时间,文字太长了不适合放在一行.打量着王五...很好... 王五, 你怎么样?张三李四王五

这将产生一个流程图。:

链接长方形圆圆角长方形菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.2.0开始我的操作确认?结束yesno
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。

  1. 注脚的解释 ↩︎

  • 点赞
  • 收藏
  • 分享
  • 文章举报
王舜琦 发布了2 篇原创文章 · 获赞 0 · 访问量 997 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: