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



我目前正在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.

以下是我自己编写函数时得到的图形:适当的窗口函数这是我使用内置函数时得到的一个:无意义函数

结论是,当信号相乘时,我不明白这是怎么回事。你能帮我理解为什么以及如何修改函数来计算吗?

问题是hamming返回的window1Nx1向量,而x1xN向量。当您对这些矩阵执行逐元素乘法时,它将生成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

相关内容

  • 没有找到相关文章

最新更新