Skip to content

A Swift playground that demonstrates the Fourier series expansion.

License

Notifications You must be signed in to change notification settings

gongzhang/swift-fourier-expansion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fourier Series Expansion in Swift Playground

https://en.wikipedia.org/wiki/Fourier_series

For example, there is an input function f that is defined on domain 0 to 1:

let domain = 0.0 ... 1.0

let f: Double -> Double = { x in
    switch x {
    case 0.0 ... 0.25: return -1
    case 0.25 ... 0.5: return 1
    case 0.5 ... 0.75: return -1
    default: return 1
    }
}

The graph of function f likes this:

Then you can expand function f into Fourier series:

var result = fourierExpand(f, domain: domain, seriesCount: 15)
result.simplify(tolerance: 0.05) // allow 5% error

To see the result, print result.expression:

And draw the graph of expanded f by calling result.function(_:):

for x in domain.start.stride(to: domain.end, by: 0.01) {
    result.function(x)
}

About

A Swift playground that demonstrates the Fourier series expansion.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages