Writing the code for Fourier Series

Job Description:

import numpy as np

import [login to view URL] as plt


ASSIGNMENT: COMPLETE fsc and fs functions below



This code plots the Fourier series representation of periodic signals




fsc takes five arguments:

1) natural number n: term index in the Fourier series

2) function g: the basis function ([login to view URL] or [login to view URL])

3) function f: the periodic function to be approximated

4) list x of values from [-pi,+pi]

5) float dx: the length between values of x in interval [-pi,+pi]

Return: integral of f(xe) * g(n * xe) / pi over interval [-pi,+pi]

Integral approx. by Riemann sum, using divisions of width dx


1. return the sum of f(xe) * g(n * xe) / pi * dx over xe in x


# Fourier series coefficient n for f using basis function g

def fsc(n, g, f, x, dx):

# 1. integral: (1/pi) f(xe) g(n xe) dx over all xe in x


# compute Fourier series coefficients: a0, a, b

def fsc_all(N_max, f, x, dx):

a0 = fsc(0, [login to view URL], f, x, dx)

a = [fsc(n, [login to view URL], f, x, dx) for n in range(1,N_max+1)]

b = [fsc(n, [login to view URL], f, x, dx) for n in range(1,N_max+1)]

return a0, a, b



fs takes five arguments:

1) x: the value of x at which the Fourier series is to be computed

2) a0: the "DC term" a_0

3) a: the list of a_n coefficients for n=1,..

4) b: the list of b_n coefficients for n=1,..

5) N: the number of terms n to be used in the representation

Return: sum of a0/2 + the sum of the a_n and b_n over n=1,..,N


1. compute a_sum: sum of a[n-1] * [login to view URL](n x) over n=1,...N

2. compute b_sum: sum of b[n-1] * [login to view URL](n x) over n=1,...N

3. return the target sum


# Fourier series representation at x using N terms

def fs(x, a0, a, b, N):

# 1. sum the a_n coefficients from 1 to N

a_sum = a[n-1]*[login to view URL](n x)


# 2. sum the b_n coefficients from 1 to N


# 3. return the Fourier series approximation


# plot_fsr: 4 subplots: f(x) and Fourier series using N1, N2, N3 terms

def plot_fsr(f, N1, N2, N3, x, dx, name, filename):

# get all Fourier series coefficients

a0, a, b = fsc_all(max(N1,N2,N3), f, x, dx)

[login to view URL]()

ay,(ax1,ax2,ax3,ax4)=[login to view URL](nrows=4, sharex=True)


[login to view URL](x, f(x))

[login to view URL](x, fs(x, a0, a, b, N1))

[login to view URL](x, fs(x, a0, a, b, N2))

[login to view URL](x, fs(x, a0, a, b, N3))

ax1.set_title('{}: f(x)'.format(name))

ax2.set_title('{}: Fourier series, N = {}'.format(name, N1))

ax3.set_title('{}: Fourier series, N = {}'.format(name, N2))

ax4.set_title('{}: Fourier series, N = {}'.format(name, N3))

[login to view URL](filename)

# sample periodic functions (signals)

def f_square(x): return [login to view URL](x >= 0, 1, 0)

def f_triangle(x): return [login to view URL](x >=0, -x, x)

def f_sawtooth(x): return x

def f_rectifier(x): return [login to view URL](x >= 0, x, 0)

# main function

if __name__ == "__main__":

# specify dx: the distance between x-values over [-pi,+pi]

dx = 1/100

# number of points in [-pi, +pi] using width dx

m = int(2 * [login to view URL] / dx)

# list of m x-axis values in [-pi, +pi]

x = [login to view URL]([login to view URL], [login to view URL], m)

# specify the three values of N (# terms in series)

N1, N2, N3 = 2, 20, 200

# call plot_fsr with four different signals defined above

plot_fsr(f_square, N1, N2, N3, x, dx, 'square','[login to view URL]')

plot_fsr(f_triangle, N1, N2, N3, x, dx, 'triangle', '[login to view URL]')

plot_fsr(f_sawtooth, N1, N2, N3, x, dx, 'sawtooth', '[login to view URL]')

plot_fsr(f_rectifier, N1, N2, N3, x, dx, 'rectifier', '[login to view URL]')

Habilidades: Python

Sobre o Cliente:
( 5 comentários ) PHILADELPHIA, United States

ID do Projeto: #33506853

Concedido a:


Hi, I have read your post carefully. I can finish your task perfectly in time. I have full experienced with python And I have also worked with these similar projects. We can discuss more detail via chatting. Thank you!

$20 USD em 1 dia
(3 Comentários)

4 freelancers estão ofertando em média $21 nesse trabalho


Hi there! Thank you for sharing your project requirement, I have carefully read the project description and I would like to take part in this project. I have the required project skills (Python) suitable for succsesful Mais

$25 USD in 4 dias
(0 Comentários)

Hello. I have reviewed your project carefully and came to the result that can carry out your project. As I am signal expert, I can fix your issue perfectly as well. I can you please give me one more chance? Thanks .Dmi Mais

$20 USD em 1 dia
(1 Comentário)