Use a Variable-Size Signal in a Filtering Algorithm
该示例使用可变大小的向量来存储白噪声信号的值。向量的大小在运行时可能会有所不同,因为功能通过以下方式修剪信号值。
Filtering out signal values that are not unique to within a specified tolerance of each other.
平均每两个信号值并仅输出结果均值。
在此模型中,带限制的白噪声块生成一组正态分布的随机值,作为白噪声信号的来源。MATLAB功能块Filter
过滤彼此指定公差内并非唯一唯一的信号值。然后,MATLAB功能块Average
outputs the average of a specified number of unique signal values. The Scope blocks display the output from theFilter
和Average
块。打开模型以查看配置。
model ="emldemo_process_signal"; open_system(model);
Inspect the Source Signal
Open the Band-Limited White Noise block to view the properties of the source signal. The size of the噪声力量parameter defines the size of the array that holds the signal values. This array is a 1-by-9 vector of double values.
Inspect theFilter
MATLAB Function Block
Open Filter
to inspect the filtering function.Filter
filters out unique signal values that are not within a tolerance of0.2
of each other. The function calls an external MATLAB® function file,emldemo_niquetol。m
, to filter the signal values. The function passes the 1-by-9 vector of white noise signal values as the first argument and the tolerance value as the second argument.
功能y = uniquify(u) y = emldemo_uniquetol(u,0.2);
Open the MATLAB function fileemldemo_niquetol。m
要查看外部功能的代码,emldemo_niquetol
。emldemo_niquetol
returns the filtered values ofA
in an output vectorB
so thatabs(B(i) - B(j)) > tol
for alli
和j
。
功能B = emldemo_uniquetol(A,tol)%#codegenA = sort(A); B = A(1); k = 1;for我= 2:长度(A)ifabs(A(k) - A(i)) > tol B = [B A(i)]; k = i;endend
At each time step, the Band-Limited White Noise block generates a different set of random values forA
, 和emldemo_niquetol
may produce a different number of output signals inB
。Therefore,y
must be variable size. In order for y to be variable-size, you must enable theVariable sizeproperty。In this example,Variable sizeis enabled fory
。InFilter
, open theSymbols窗格和物业检查员。在里面功能tab, clickEdit Data。在里面Symbolspane, clicky
查看属性检查员中的属性。对于可变大小的输出,您必须指定Sizeproperty as the maximum-size upper bound. In the example,Sizeis[1 9]
。
You must enable theSupport variable-size arraysMATLAB功能块中的属性以启用Variable size参数输出。
Inspect theAverage
MATLAB Function Block
Average
平均值通过Filter
通过遵循这些条件:
If the number of signals is greater than
1
和divisible by2
, thenAverage
averages every consecutive pair of values.If the number of signals is greater than
1
but not divisible by2
, thenAverage
删除第一个值并平均其余连续对。If there is exactly one signal, then
Average
returns the value unchanged.
OpenAverage
to view the code.
功能y = avg(u)ifnumel(u)== 1 y = u;elsek = numel(u)/2;ifk ~= floor(k) u = u(2:numel(u));endy = emldemo_navg(u,2);end
Theavg
功能calls the external MATLAB functionemldemo_navg
to calculate the average of every two consecutive signal values.
功能b = emldemo_navg(a,n)%#codegenassert(n>=1 && n<=numel(A)); B = zeros(1,numel(A)/n); k = 1;fori = 1 : numel(A)/n B(i) = mean(A(k + (0:n-1))); k = k + n;end
两个都u
和y
are variable-size. You do not need to explicitly defineu
as variable-size, becauseu
是输入。输出y
is declared as a variable-size vector because the number of elements varies depending on the size provided byu
。检查特性y
to confirm that it is variable-size.
Simulate the Model
Simulate the model to view the results in each Scope block.Filter
输出一个variable number of signal values each time it executes.
Average
输出一个variable number of signal values each time it executes. The block returns approximately half the number of the unique values.