0
$\begingroup$

I have this Matlab/Octave code:

% needed for Octave ----------------------    
pkg load signal
% ------------------------------------------------

% Square -----------------------------------------
figure
N=50;                               % harmonics
fs = 1000;
r_start=0;
r_end=10;
r_step=1/fs;
r = r_start:r_step:r_end;           % range
w_sqr = square(r)/2;                % Square wave [-0.5:0.5]
%w_sqr = square(r);                  % [-1:1]


plot(r,w_sqr);
axis([r_start r_end -1.2 1.2]);
grid on;
hold on;

% Fourier -----------------------------------------
i=1;
sum=0;
for t=r
  for n=1:N
    sum = sum + (2*sin(n*t)+sin(pi*n-n*t)-sin(n*t+pi*n))/(2*pi*n);
  end
  F(i)=sum;
  i=i+1;
  sum = 0;
end;
F=F';
plot(r,F);
axis([r_start r_end -1.2 1.2]);

which results this:

enter image description here

By changing the value of variable w_sqr to (square(r)+1)/2 and calculation of F to F(i)=(1/2)+sum I get the result in range [0:1].

What changes are needed to do in Stage 2 to get the result in range [-1:1]?

$\endgroup$
1
$\begingroup$

I guess f(i) = 2*sum should do:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yes, this was what I thought but, got me suspecting if it is OK that the "variation at peak levels" grows a bit? $\endgroup$ – Juha P Aug 15 at 8:39
0
$\begingroup$

After playing a bit more with the code I probably managed to find another solution. Calculating f as f(i)=sign(sum)*(1/2)+sum gives proper looking plot:

enter image description here

and which has little less variation at peak levels compared to solution in Matt L's answer.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.