0

let us consider following equation

x(t)=sum(a(i)*sin(2*pi*f(i)*t+b(i)*cos(2*pi*f(i)*t))

where i=1,2,......m and frequencies f=[f1,f2,.....fm] and t=[t1,t2,....tn]

i want to create matrix by sin(2*pi*f(i)*t) and cos(2*pi*f(i)*t),clearly it would be matrix with dimension NX2*m,i have tried following code

function [amplitudes]=determine_amplitudes(y,f,t,n,m);

X=zeros(n,2*m);
for i=1:n
     for k=1:m
      if mod(k,2)==1 
          X(i,k)=sin(2*pi*f(k)*t(i));
      else
           X(i,k)=cos(2*pi*f(k)*t(i);
      end

     end
end
end

i used mod operator to determine that if k is odd index,then there should be written sin value,else cosine value,but problem is that i am not sure that given matrix would be with dimension NX2*m,so how to create such matrix so that not exceed index of bounds of frequency array ,recall that frequency array is following f=[f1,f2,..fm],so my problem simple is how to apply m frequency at 2*m position,thanks for help

UPDATE:

let say m=3, and frequencies f=[12.5 13.6 21.7]

then we have following matrix ,also assume

 n=4 t=[0.01 0.02 0.03 0.04]

 sin(2*pi*f(1)*t(1))  cos(2*pi*f(1)* t(1)) sin(2*pi*f(2)*t(1)) cos(2*pi*f(2)*t(1)) sin(2*pi*f(3)*t(1)) cos(2*pi*f(3)*t(1))
7
  • I think the final matix would be of size nXm because with the if conditional it's just selecting one of sin or cos terms, isn't that right? Commented Mar 21, 2014 at 5:51
  • no no,select sin or cosine yes,but ok i will update my question to clarify it Commented Mar 21, 2014 at 5:53
  • i have updated please see and thanks for reply Commented Mar 21, 2014 at 5:58
  • sorry there must be 2*m components,there is no space,it should sin(f(1) cos(f(1)) sin(f(2) cos(f(2)) and so on Commented Mar 21, 2014 at 5:59
  • f has three values,how can it have f(4)? Commented Mar 21, 2014 at 6:01

1 Answer 1

1

No loop version

[f1,t1] = meshgrid(f',t');

p1 = sin(bsxfun(@times,f1,2*pi*t')); %%// Create sin copy '
p2 = cos(bsxfun(@times,f1,2*pi*t')); %%// Create cos copy '

d1 = [p1;p2];
final_matrix = reshape(d1,size(p1,1),[]);

Naive loop version

final_matrx2 = zeros(n,2*m);
for k1=1:n
    for k2=1:2*m
        if rem(k2,2)==1 
            final_matrx2(k1,k2) = sin(2*pi*f(ceil(k2/2))*t(k1));
        else
            final_matrx2(k1,k2) = cos(2*pi*f(ceil(k2/2))*t(k1));
        end
    end
end
Sign up to request clarification or add additional context in comments.

2 Comments

please if it possible using loops?it would be more readily for me
no i have not tested this code,just need to have code like i have posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.