为什么MATLAB内置函数结果与我使用相同表达式自定义函数时得到的结果不同?

Edw*_*kdd 2 matlab built-in matlab-figure

我目前正在对matlab中的某些信号进行频谱分析,我应该使用Hamming窗口函数(https://www.mathworks.com/help/signal/ref/hamming.html)进一步分析相同的信号但是我我遇到了一些不寻常的问题,当我使用内置函数hamming(L)时得到错误的结果,当我自己编写函数时,得到正确的结果,就像在MATLAB中定义一样.这是代码:

%This is a signal i am analyzing and it has N points.
F1 = 55/450;
F2 = 67/450;
N = 450;
n = 0:(N-1);
x = 50*cos(2*pi*F1*n)+100*cos(2*pi*F2*n)

% Here is the code for plotting the signal in N points
figure
n = 0:(N-1);
stem(n,x);
xlabel('n');
ylabel('x[n]');
title('Discrete Signal x[n]');
pause

% Here i am using a built in function. It is of length N, the same as x[n].
n=0:(N-1);
window1 = hamming(N);
figure
y1 = x.*window1; %The proper way of windowing a function.
stem(n, y1);
title('Windowed Discrete Signal y[n]');
pause
% This yields some nonsensical graph even though the window function itself graphs properly. 

% Here i looked at the expression on the site which is used for the built-in matlab function and wrote it myself.
n=0:(N-1);
window2 = 0.54-0.46*cos(2*pi*n./N);
figure
y2 = x.*window2;
stem(n, y2);
title('Windowed Discrete Signal y[n]');
pause
% Here it graphs everything perfectly.
Run Code Online (Sandbox Code Playgroud)

这是我自己编写函数时得到的图形图像: 正确的窗口函数 这里是我使用内置函数时得到的图形: 无意义函数

结论是,我不理解在将信号相乘时的工作方式.你能帮我理解为什么以及如何修改功能来解决问题?

Mat*_*att 6

问题是window1,返回的hamming是一个Nx1载体,同时x是一个1xN载体.当你对它们执行逐元素乘法时,它将产生一个N*N矩阵.请参阅下面的示例.如果你重塑一个window1x那样形状匹配(例如y1 = x.*window1';),你会得到你想要的结果

>> a = [1 2 3]

a =

     1     2     3

>> b = [1; 2; 3]

b =

     1
     2
     3

>> a.*b

ans =

     1     2     3
     2     4     6
     3     6     9
Run Code Online (Sandbox Code Playgroud)