Forums > MaxMSP

Getting frequency and amplitude values using FFT~

February 11, 2013 | 7:36 pm

Hello,

Is there away for getting frequency and amplitude values out of fft~?
if so would it be possible for some direction?


February 11, 2013 | 9:03 pm

I think you want to spend some time with MSP tutorial #26. Also look at #25.

Quickly, the frequencies of individual fft bins don’t change. Each bin represents a bandwidth of (Sample Rate)/(total number of samples). So, a 1024 sample fft has 512 frequency bins of ~43 HZ each — at 44.1k samples per second. fft~ and pfft~ output a stream of bin numbers.

For amplitude: if you run the real and imaginary outputs of fft~ or pfft~ into cartopol~, the left outlet of cartopol~ is the amplitude for that specific bin.


February 11, 2013 | 11:51 pm

Maybe this is somewhat helpful:

– Pasted Max Patch, click to expand. –

February 16, 2013 | 3:26 pm

I’m also having issues with this… mzed, you’re example patch didn’t seem to work for me!
I’m trying to use the amplitude values that are output to obtain the frequency with the highest amplitude value – to then use generate a saw wave with that frequency… any ideas on how I would go about this?


February 16, 2013 | 9:39 pm

This is rather messy, but seems to work. Using gen~.

– Pasted Max Patch, click to expand. –

February 16, 2013 | 10:00 pm

And without gen~…

– Pasted Max Patch, click to expand. –

One cheap-o pitch tracker!


March 1, 2013 | 3:32 pm

Thanks very much!! Now just got to figure out how that works! :)


May 30, 2014 | 3:47 pm

Hey all,

I know its been a long time since the last post but I hope I will get an answer. So i am using exactly the same patch that MZED posted (without gen), however I have a signal with sampling rate 256Hz. Do you have any idea what changes should I do so as to work properly? I suppose my question is mostly for MZED but any help is welcomed.

Cheers


May 30, 2014 | 4:23 pm

256 Hz is a crazy-low sampling rate. If you’re running at a sample rate that Max supports, then my patch should be fine. The sample rate is reported by the dspstate~ object on the right.


May 30, 2014 | 4:50 pm

Let me make that more clear. The sampling rate in the audio setup menu is of course set at 44100kHz. However I want to do the same task with your patch but with an EEG data signal which has sampl. rate = 256Hz.


May 30, 2014 | 4:59 pm

This particular patch is doing realtime fft~ with Max and only makes sense in that context. I’m not sure how you’re getting samples from your device into Max.

In general, the frequency for each bin is approximately: (bin number) * ((sample rate)/(number of bins))


May 30, 2014 | 5:11 pm

"My" patch is completely same to yours but instead of having that sound generator I have a .wav sample with s.r. = 256Hz played. The problem is that I get mostly zero-frequency as exit, no matter how much I decrease the bin’s range.

  • This reply was modified 3 months by  seabed.

June 2, 2014 | 3:20 pm

Ηey MZED can you please explain to me how subpatch "find_bin" works? I think that any changes I have to do, should be in there. Thanks


June 2, 2014 | 3:27 pm

At the end of each fft (edge~) it uses Uzi to scan the buffered results. For each result, the bin index is stored in the first "i" object. If the amplitude in that bin is the highest so far (tested by peak~), the bin index is banged into the second int object. When Uzi is done, it bangs the final result out of that same int object.

If you post your patch, I could help some more.


June 4, 2014 | 1:49 am

Hey, here is my patch. I load .wav file dok1 (it is attached) to the buffer and then its the same process. The only changes are:

1. Instead of dpstate, I use the fixed number 256 (you should enter it in the number box before the patch starts)
2. I used cycle instead of saw
3. I multiply the outcome with 250 just to have a more audible frequency

The problem is that most of the time the exit is zero frequency. Moreover the frequency resolution should be around 0.063 Hz (sampling rate/fft size) but it doesn’t seem like changes of that scale are perceptible.

Edit:Actually for a safety reason I cant upload the wav file so here it is a link for it

http://speedy.sh/jMVxd/dok1.rar

– Pasted Max Patch, click to expand. –
  • This reply was modified 2 months by  seabed.

June 12, 2014 | 1:29 pm

So? Any new thoughts? I am really stuck at this part.


June 12, 2014 | 2:52 pm

play~ is going to be playing back samples from the buffer at whatever the sample rate is set to in Max. So, if your samples were recorded at 256k, then pitch is going to be very high. I think you’d want to divide multiply the estimated pitch (right before the saw~) by ((.wav file sample rate)/(current max sample rate)).

I’m still kind of confused by what you’re doing though, so I could be giving you the wrong answer.


June 13, 2014 | 8:12 am

Hey, first of all thanks for all this help.

Well, what I am trying to do in a few words is a frequency scaling. I have an EEG data signal which is a .wav file at 256Hz (sampling rate). With your patch I am trying to find the frequency with the highest amplitude value and then scale it to an audible range of 250Hz – 22kHz. Your patch works perfectly for a sample with S.R. = 44100Hz, but not so good for S.R. = 256Hz. What can I do?


June 20, 2014 | 4:24 am

Hi again,

I have also noticed that defining the length of buffer "spectrum" at samples (buffer spectrum @samps 2048) is not compatible with every Max version. Can I do it in another way?


July 29, 2014 | 11:33 am

Hey MZED, I know it’s been a long time but I hope I’ll get an answer.

I am using your patch that finds the central frequency of a bin with maximum amplitude. I mostly interested in lower frequencies such as 4-30Hz. However is not possible to have a resolution lower than 10.77Hz as long as the range of every bin is 10.77 (samp.rate = 44100/fft_size = 4096). As far as I know Max doesn’t support fft size bigger than 4096. So, what can I do to have a better frequency resolution?


August 7, 2014 | 7:52 am

Once again about this lovely patch.

I noticed that it’s not stable for frequencies higher than 5kHz. You can try an oscillator at 10kHz, the maximum output frequency is stuck at 5502 Hz. Any idea why is this happening?

EDIT: It seems like the uzi object needs to be same size with the fft size.

  • This reply was modified 3 weeks by  seabed.

August 7, 2014 | 9:30 am

Good catch, thanks. Actually, it needs to be the same as the buffer size, which is (fft size / 2). The second half of the fft~ output is a mirror of the first half.


Viewing 22 posts - 1 through 22 (of 22 total)