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 commentaires

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

Connectez-vous pour commenter.

Réponses (2)

Guillaume
Guillaume le 9 Fév 2015
Modifié(e) : Guillaume le 9 Fév 2015
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 le 9 Fév 2015
This should solve the different size issue:
t = linspace(tstart, tstart+dur, numel(Xk));

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by