主要内容

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功能块Averageoutputs the average of a specified number of unique signal values. The Scope blocks display the output from theFilterAverage块。打开模型以查看配置。

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 theFilterMATLAB Function Block

Open Filterto inspect the filtering function.Filterfilters out unique signal values that are not within a tolerance of0.2of 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_niquetolemldemo_niquetolreturns the filtered values ofAin an output vectorBso thatabs(B(i) - B(j)) > tolfor allij

功能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_niquetolmay produce a different number of output signals inB。Therefore,ymust be variable size. In order for y to be variable-size, you must enable theVariable sizepropertyIn 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 theAverageMATLAB Function Block

Average平均值通过Filter通过遵循这些条件:

  • If the number of signals is greater than1和divisible by2, thenAverageaverages every consecutive pair of values.

  • If the number of signals is greater than1but not divisible by2, thenAverage删除第一个值并平均其余连续对。

  • If there is exactly one signal, thenAveragereturns the value unchanged.

OpenAverageto 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_navgto 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

两个都uyare variable-size. You do not need to explicitly defineuas variable-size, becauseu是输入。输出yis declared as a variable-size vector because the number of elements varies depending on the size provided byu。检查特性yto 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.

See Also

|

相关话题