答案1
我会给你一个想法pgfplots
和declare function
Ti 的设施钾Z。
我定义了一个函数(使用三元函数的分段方法test ? true value : false value
)及其或多或少的导数,然后定义了一个“假”噪声函数;说是假,是因为它只是一个正弦波,其中添加了一点随机性,以便在视觉上模拟噪声。
我还使用样式来最大限度地减少重复并将常见的定义集中在一个站点中。
\documentclass[border=10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=newest}% change to the current release
\begin{document}
\pgfplotsset{
base plot/.style={% common style for the plots
width=10cm, height=5cm,
xmin=0, xmax=10,
domain=0:10,
samples=100,
axis x line = center,
axis y line = center,
enlarge x limits,
enlarge y limits,
xlabel = {$x$},
% not used, but...
legend style = {nodes={right, font=\scriptsize}, at={(0.05,0.6)}, anchor=west},
clip mode = individual,
}}
\begin{tikzpicture}[declare function={
% constant-ramp-constant function
xrampz(\x)=\x<2 ? 0 : (\x<6 ? \x-2 : 4);
% generic derivate of the above
Dxrampz(\x)=\x<2 ? 0 : (\x<6 ? 1 : 0);
% pseudo-noise function, created "by feel"
fakenoisesin(\A,\f,\x)=\A*(0.6*rand+sin(\f*\x));
}]
\begin{axis}[
base plot,
ymin=-1, ymax=5,
ylabel = {$y$},
]
\addplot[thick] {xrampz(x)+fakenoisesin(.1,400,x)};
\addplot[red, thin] {xrampz(x)};
\end{axis}
\begin{axis}[
base plot, yshift=-4cm,% move it down
ymin=-1, ymax=2,
ylabel = {$y'$},
]
\addplot[thick] {Dxrampz(x)+fakenoisesin(.2,400,x)};
\addplot[red, thin] {Dxrampz(x)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
只是为了好玩:蓝线是随机白噪声,红线已经经过 5 点加权移动平均滤波器处理。
为了提高速度和准确性,人们确实应该使用 C++ 或 matlab 来完成此操作。
\documentclass[border=10pt]{standalone}
%\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=newest}% change to the current release
\usepackage{pgfplotstable}
\begin{document}
\pgfmathsetmacro{\noise}{rand}
\foreach \x in {1,...,50} {\pgfmathsetmacro{\y}{1.0*rand}%
\xdef\noise{\noise,\y}}%
\edef\noise{{\noise}}% pgfmath array
\pgfmathsetmacro{\ma}{0.375*\noise[0]+0.25*\noise[1]+0.0625*\noise[2]}%
\pgfmathsetmacro{\y}{0.25*\noise[0]+0.375*\noise[1]+0.25*\noise[2]+0.0625*\noise[3]}%
\xdef\ma{\ma,\y}%
\foreach \x in {2,...,48} {\pgfmathsetmacro{\y}{0.0625*\noise[\x-2]+0.25*\noise[\x-1]+0.375*\noise[\x]+0.25*\noise[\x+1]+0.0625*\noise[\x+2]}%
\xdef\ma{\ma,\y}}%
\pgfmathsetmacro{\y}{0.0625*\noise[47]+0.25*\noise[48]+0.375*\noise[49]+0.25*\noise[50]}%
\edef\ma{\ma,\y}
\pgfmathsetmacro{\y}{0.0625*\noise[48]+0.25*\noise[49]+0.375*\noise[50]}%
\edef\ma{{\ma,\y}}
\pgfplotstablenew[columns={x,random,banded},
create on use/x/.style={create col/set list={0,1,...,50}},
create on use/random/.style={create col/set list/.expand once=\noise},
create on use/banded/.style={create col/set list/.expand once=\ma}]
{51}\mytable
%\pgfplotstabletypeset{\mytable}
\begin{tikzpicture}
\begin{axis}
\addplot[blue] table[y=random] \mytable;
\addplot[red] table[y=banded] \mytable;
\end{axis}
\end{tikzpicture}
\end{document}