MATLAB: How to add harmonics to a signal ? Help please !!

frequencyMATLABsignal processing

Is there anyone who can help me?
I need to add harmonics of a signal to that signal. Signal is given in vectorized form and it is a discrete signal. If fundamental frequency is w0, it will be 2*w0 for the first harmonic. But how can I find that signal with 2*w0 frequency ?
my samples of the signal s (in vectorized form): -0.239570299386578 0.1034882656331065 0.434308908352916 0.7137668996246224 0.9088106936857204 0.9963988158818323 0.9661854915005951 0.8217108676412244 0.5800958281197547 0.2698446607867672 -0.07226783043916135 -0.4058656575212867 -0.6914578691976684 -0.8952909939878536 -0.9932554094058046 -0.9737846003601184 -0.8391674550614948 -0.6053346354564043 -0.2999053926206244 0.04098635822626423
it's period = 18. I want to find the amplitute's of the signal with period 9 by using this values.. w0=0.34907 given. There are 16000 samples I have copied only 20 of them.
here is my code :
function [ans] = Harmonic(s,w0,k) % function to add harmonics to a discrete signal, s is the vector of input signal, w0 is frequency, k is the kth harmonic
scaled_N=2*pi/(w0*k); % period for the kth harmonic
scaled=[];
x=numel(s)/k; % sample length for the signal whose kth harmonic is calculated
for m=1:1:x
scaled(m,1)=s(k*m,1); % this finds the kth harmonic
end
s_m=1;
for n=1:1:numel(s)
if(s_m==x+1) % scaled has x elements when finished turn to back
s_m=1;
end
ans(n,1)=s(n,1)+scaled(s_m,1); % addition of input signal to the harmonic calculated form is the answer
s_m=s_m+1;
end
endfunction
Another information to control: answer for the 2nd harmonic is : -0.4651631214331491 0.2058778649250771 0.78240302743614 0.9998168889431442 0.7583239234595782 0.1687673574022643 -0.4982757042146062 -0.9365825373088779 -0.9450361644337291 -0.5196996978667562 0.1441694387646107 0.7418744468520158 0.9990539262062441 0.7976622821741386 0.2301705984679712 -0.4429761650440993 -0.9128086184270765 -0.9636524552140873 -0.5721915341654712 0.08191167943357644
But I'm stuck and cannot reach to the answer Thanks in advance.

Best Answer

Does the following function do what you want?
function y = harmonic(x,k)
% assumes that x is a column vector
assert(size(x,1) > 0);
assert(size(x,2) == 1);
% Interpolate by k and add k-th harmonic:
y = interp(x,k) + repmat(x,k,1);
end
HTH.
Rick