Home > Enterprise >  Changing amplitude of fourier series in matlab
Changing amplitude of fourier series in matlab

Time:08-07

The code below currently plots the fourier series for a square wave for N terms. Is there any way I could change the range from [0;1] to [-1;1]?

% Assignment of variables
syms t 

% Function variables    
N = 5;
T0 = 1;
w0 = 2*pi/T0;
Imin = 0;
Imax = 0.5;

% Function
ft = 1;

% First term calculation
a0 = (1/T0)*int(ft, t, Imin, Imax);
y = a0;

% Calculation of n terms    
for n = 1:N        
    an = (2/T0)*int(ft*cos(n*w0*t), t, Imin, Imax);    
    bn = (2/T0)*int(ft*sin(n*w0*t), t, Imin, Imax);    
    y = y   an*cos(n*w0*t)   bn*sin(n*w0*t);
end

fplot(y, [-4,4], "Black")
grid on

CodePudding user response:

If you are talking about the figure scale, then ylim([-1 1])

CodePudding user response:

1.- The following does what you asked for:

    clear all;clc;close all
syms t 
assume(t>0 & t<1)

% Function variables    
N = 5;
T0 = 1;
w0 = 2*pi/T0;
Imin = 0;
Imax = 1;

% Function
h1=heaviside(t-.5)
h2=heaviside(t .5)
ht=-2*((h1-h2) .5)

% First term calculation
a0 = (1/T0)*int(ht, t, Imin, Imax);
y = a0;

% Calculation of n terms    
for n = 1:N        
    an = (2/T0)*int(ht*cos(n*w0*t), t, Imin, Imax);    
    bn = (2/T0)*int(ht*sin(n*w0*t), t, Imin, Imax);    
    y = y   an*cos(n*w0*t)   bn*sin(n*w0*t);
end

fplot(y, [-4,4], "Black")
grid on

2.- You allocate a specific group of code lines headed with % Function to precisely define the function.

Yet you actually define the function with Imin and Imax.

It's good practice to constrain the function definition within the lines you intend for such purpose, not to scatter the function all over the place.

  • Related