Error- using --- .* -- Matrix dimensions must agree.

Hello everyone I am new to Matlab and have a simple question. The error I am receiving is one that I don't fully understand. I have tried changing the .* to a * in my equation but still come up with the same error. I am sure it is something that I am completely overlooking and cant figure it out any help would be appreciated. My error is in x = Xk .* cos((fk * pi) .* t);
function [x,t] = syn_sin(fk,Xk,fs,dur, tstart)
%SYN-SIn FUnction to sythesize a sum of cosine waves
% Usage:
% [xx,tt] = syn_sin(fk,Xk,fs,dur,tstart)
% fk = vector of frequencies
% (these could be negative or positive)
% Xk = vector of complex amplitudes: Amp*e*exp(j*phase)
% fs = the number of samples per second for the time axis
% dur = total time duration of the signal
% tstart = starting time(default is zero, if you make this input
% optional)
% xx = vector of sinusoidal values
% tt = vector of times, for the time axis
%
%
% note: fk and XK must be the same length.
% Xk(1) = corresponds to frequency in fk(1),
% Xk(2) = corresponds to frequency in fk(2), etc
t = (tstart:1/(fs):dur);
x = Xk .* cos((fk * pi) .* t);
size(Xk)
size(fk)
plot (x,t);
shg
the size of Xk = 1 3
the size of fk = 1 3
I figured I would check the dimensions.

댓글 수: 2

Erik S.
Erik S. 2015년 2월 9일
what is the size of t?
Guillaume
Guillaume 2015년 2월 9일
Next time, select your code and click on the {} Code button to format it properly, like I've done for you this time.

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2015년 2월 9일
편집: Guillaume 2015년 2월 9일

1 개 추천

If xk and fk have the same size, then obviously, it's the third vector t that is a different size.
I don't see how you guarantee that t is the same length as Xk and fk. Moreover, your t calculation looks wrong, you're going from a point in time tstart to a duration dur. Maybe you meant:
t = tstart:1/fs:tstart + dur;
This still does not guarantee that t has the same number of elements as xk.
Stephen23
Stephen23 2015년 2월 9일

0 개 추천

This should solve the different size issue:
t = linspace(tstart, tstart+dur, numel(Xk));

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

태그

질문:

2015년 2월 9일

댓글:

2015년 2월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by