Fast Fourier transform in APL

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

Fast Fourier transform in APL

Postby ray on Tue Jul 09, 2024 4:08 pm

I have had some success in creating music from scratch just using Dyalog APL. So far, I have manage to reproduce (to some extent) the sound of Organ pipes, Piano and a Guitar string.

I wish to add some more (and improve the quality of my existing) instruments.

Currently I create an instrument sound of a note by adding to the fundamental frequency, multiple harmonics of that frequency with various amplitudes (and than apply a sound envelope to the whole).

However I have failed to find the amplitudes and frequency data for many instruments of sufficient quality on the internet to meet my needs.

My understanding is that I "should" be able to via Fast Fourier Transforms, analysis a sound file to get the harmonics and amplitudes information I need.

My idea is to produce (via a Midi interface) the sounds of different instruments playing a single note of know frequency to use as my sound source.

At this point, it all falls apart, as I have no idea how to write (or use) an FFT in APL!

Can anyone offer any help with FFT in APL?

Thanks

Ray
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 238
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: Fast Fourier transform in APL

Postby PGilbert on Wed Jul 10, 2024 12:11 pm

Hello Ray, I am not an expert in Fourier Transform but here is what I have done:

      ∇ X←DFT x;k;n;N
⍝ Discrete Fourier Transform
https://en.wikipedia.org/wiki/Discrete_ ... _transform

⍝ DFT←{⍵+.×⍨*○0J¯2×n÷⍨∘.×⍨¯1+⍳n←≢⍵} APLCart WS Full if too big

⍝ For x←1 2J¯1 0J¯1 ¯1J2 then X = 2 ¯2J¯2 0J¯2 4J4
⍝ Angular frequency of the points of the DFT = n×(2×PI)÷(N×∆t)

N←≢x ⍝ Size of vector to transform
X←⍳0 ⍝ Placeholder for the result
n←¯1+⍳N ⍝ index generator

:For k :In n
X,←+/x×(*○0J¯2×k×n÷N)
:EndFor


∇ x←IDFT X;k;n;N
⍝ Inverse Discrete Fourier Transform
https://en.wikipedia.org/wiki/Discrete_ ... _transform

⍝ IDFT←{n÷⍨⍵+.×⍨*○0J2×n÷⍨∘.×⍨¯1+⍳n←≢⍵} APLCart WS Full if too big

⍝ For X = 2 ¯2J¯2 0J¯2 4J4 then x←1 2J¯1 0J¯1 ¯1J2

N←≢X ⍝ Size of vector to transform
x←⍳0 ⍝ Placeholder for the result
n←¯1+⍳N ⍝ index generator

:For k :In n
x,←(÷N)×+/X×(*○0J2×k×n÷N)
:EndFor


⍝ For complex number:
Re←{9○⍵} ⍝ Real part
Im←{11○⍵} ⍝ Imaginary part
Mag←{|⍵} ⍝ Magnitude
Phi←{12○⍵} ⍝ Phase


The https://aplcart.info/ has one liner but you may get a WS full if you have too much data, so I have made a function with iteration.

Good luck.
User avatar
PGilbert
 
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Fast Fourier transform in APL

Postby ray on Wed Jul 10, 2024 5:58 pm

Thank you so much for this.

I will let you know how I get on with it.

Ray
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 238
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: Fast Fourier transform in APL

Postby Adam|Dyalog on Sun Jul 14, 2024 9:02 am

You can also use the fastest fourier transformation in the world, available through a library: https://github.com/dyalog/math?tab=read ... le#fourier
User avatar
Adam|Dyalog
 
Posts: 143
Joined: Thu Jun 25, 2015 1:13 pm

Re: Fast Fourier transform in APL

Postby Bob Armstrong on Sun Jul 21, 2024 5:28 pm

Being able to write a definitional Fourier transform , not fast , in one line of APL was quite instrumental in the course of my life , http://www.cosy.com/views/sycofiz.htm . ( The one line transform in the paper on that page struck Christopher Zeeman as excessively cryptic when I tried to explain it token by token . Failed to impress him with the revolution APL was at the time . I think he had a bit of a point with the
Code: Select all
 1 2 ∘.○
)

I was motivated to read Cooley&Tukey's paper at that time and wanted to express the algorithm as operations across an array of the dimensions of the prime factors of the total data length . That lines up all the redundancies in calculation so they can be simply reduced across .

I thought I noticed some additional redundancies which were not exploited by the algorithm , but had , to say the least , other priorities and never got a round toit .

That http://www.fftw.org/ site looks very interesting & impossible to beat . I'd be particularly interested in how they optimize the algorithm on prime sizes . The fact that it handles multiple dimensions is also compelling .
User avatar
Bob Armstrong
 
Posts: 27
Joined: Wed Dec 23, 2009 8:41 pm
Location: 39.038681° -105.079070° 2500m


Return to APL Chat

Who is online

Users browsing this forum: Bing [Bot] and 0 guests