Error- using --- .* -- Matrix dimensions must agree.
Show older comments
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.
Answers (2)
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.
1 Comment
Jonathan Herron
on 9 Feb 2015
Stephen23
on 9 Feb 2015
This should solve the different size issue:
t = linspace(tstart, tstart+dur, numel(Xk));
1 Comment
Jonathan Herron
on 9 Feb 2015
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!