Hec46
Hec46

Reputation: 191

Sum of sine functions in Matlab

I have to create a function with the following definition:

function [ s1,s2,sums ] = sines( pts,amp,f1,f2 )

The output argument s1 is a row vector whose length (number of elements) equals pts. The elements of s1 are the values of the sine function when it is given equally spaced arguments that start at zero and extend through f1 periods of the sine. The amplitude of the sine wave equals amp. The vector s2 is the same as s1 except that s2 contains f2 periods. The vector sums is the sum of s1 and s2. If f2 is omitted, then it should be set to a value that is 5% greater than f1. If f1 is omitted also, then it should be set to 100. If amp is not provided, then it should default to 1. Finally, if pts is omitted as well, then it should be set to 1000. I have done the following:

function [ s1,s2,sums ] = sines( pts,amp,f1,f2 )

if nargin == 3
    f2  = f1 + (f1*0.05);    
elseif nargin == 2
    f1  = 100;
    f2  = f1 + (f1*0.05);    
elseif nargin == 1
    amp = 1;
    f1  = 100;
    f2  = f1 + (f1*0.05);
elseif nargin == 0
    pts = 1000;
    amp = 1;
    f1  = 100;
    f2  = f1 + (f1*0.05);
end

t = 0:pts-1;
s1   = amp * sin( 2*pi*f1.*t );
s2   = amp * sin( 2*pi*f2.*t );
sums = s1 + s2;

end

The result should look like a frequency modulated sine function, but when I plot sums it does not look like a sine function, it looks more like a sawtooth function, very sharp. What am I doing wrong?

Upvotes: 0

Views: 1976

Answers (1)

Forss
Forss

Reputation: 778

You have forgotten to normalize your time-steps t to between 0-1. To get f1 and f2 periods for s1 and s2 respectively you should have

t = (0:pts-1)/(pts-1);

Upvotes: 1

Related Questions