result of FFT

Serg Gini kornburn at yandex.ru
Tue Jul 8 19:59:39 UTC 2025


On Tuesday, 8 July 2025 at 18:11:27 UTC, Matthew wrote:
> Hi,
> What do the 4096 resulting complex numbers represent?
> How should I use the result to check whether the 1209Hz, 
> 1336Hz, 1477Hz, or 1633Hz tones are present in that part of the 
> sound?
>
> Thanks,
> Matthew

The result of FFT should be the same as in NumPy[1] I suppose.
So D's module doesn't have the functionality that you want, so 
you need to write it by yourself.

chatGPT showed this python code, that you can port to D:
```python
n = len(data)
fft_result = np.fft.fft(data)
frequencies = np.fft.fftfreq(n, d=1/fs)
magnitude = np.abs(fft_result)
# Frequencies to detect (rounded to avoid precision issues)
dtmf_freqs = [697, 770, 852, 941, 1209, 1336, 1477]

# Frequency resolution
resolution = 5  # ±5 Hz tolerance

# Find peaks near DTMF frequencies
detected_freqs = []
for target in dtmf_freqs:
     idx = np.where((frequencies > target - resolution) & 
(frequencies < target + resolution))
     if np.any(magnitude[idx] > np.max(magnitude) * 0.1):  # 
adjust threshold if needed
         detected_freqs.append(target)

print("Detected DTMF frequencies:", detected_freqs)
```

There is no command for `fftfreq` in D, but you can write your 
own implementation, based on the NumPy documentation[2].

Also you can be interested in some other packages and examples:
- audio-formats[3] for reading and decoding different formats
- NumPy-similar package example, which is similar to NumPy 
functionality[4]

 From my perspective - solve it in NumPy will be safer approach, 
but it should be doable in D as well.

References:
[1] 
https://numpy.org/doc/2.1/reference/generated/numpy.fft.fft.html
[2] 
https://numpy.org/doc/2.1/reference/generated/numpy.fft.fftfreq.html
[3] https://github.com/AuburnSounds/audio-formats
[4] 
https://github.com/libmir/numir/blob/master/example/audio_separation/source/app.d


More information about the Digitalmars-d-learn mailing list