I ve been trying to figure out how i can get a simple Fourier analysis (frequency - amplitude in x-y axis) or in pairs in a matrix (ex
0 228Hz 78 --> (78/128 midi volume)
1 456Hz 67
2 912Hz 55
.................etc.
of complex input signal made from several oscillators and mp3 sounds.
I cannot figure out if i must use fiddle~, rfft~, or any other abstraction.
Any suggestions...?
-
Simple fft spectral
-
Take a look at E01.spectrum.pd in the help browser.
-
thanx. I must have done something wrong cause I cannot find E01.spectrum.pd in my mac. I also tried and redownload pd - extended. Nothing.
Anyway, I want to use rfft~ to spectrum in processing via libpd, so i just made a sub-patch with block size == 16 and write the outlets of [rfft~] into two arrays. Then I inserted these arrays into processing ( real[] and imginr[] ) and used the formula sqrt(real_*real_+ imgnr_*imgnr_) / 2*16 to calculate amplitudes and i also calculated frequencies by the type f(i) = i * samplerate / blocksize.
My question has little to do with these.Why i am getting prices for two or three oscillators (frequencies) even though I use only one [osc~]. In this case the one will surely be the frequency of my [osc~ ] but moreover, when I use two [osc~] 's and more I get prices that are not the frequencies of the oscillators that been used.
Is that write?____ -
Your blocksize is very small. rfft~ will only give a single non-zero amplitude for a single osc~ input if an integer number of periods of the osc signal fits exactly into your blocksize. i.e. you should get what you expect for osc~ frequencies of samplerate/16, 2*samplerate/16, etc. Otherwise you will have discontinuities at the beginning and end of your block that will lead to spectral broadening.
A simpler way to get the rfft~ magnitude without intermediate arrays is:
[osc~]
|
[rfft~]
|\ |\
[*~] [*~]
\ /
[+~]
|
[sqrt~] -
Thanx guys for the answers, but still I cannot figure out some things...
First of all, I do use the above "type" described by Emacpher, but in processing. Generally, I followed Frank Barknecht's tutorial ( http://footils.org/2010/05/24/beginners-guide-fft-objects-pd/ ), and normalize magnitude prices as the tutorial says. So I think I don't have problem with that.
On the other issue i'm facing, you say that [rfft~] will give me magnitudes for specific frequencies, and their price depends on block size, so if i have
block size == 2 & samplerate == 100 , I will get magnitudes for the frequencies:
0Hz & 50Hz
but if I have
blocksize == 10 & samplerate == 100, i will get magnitudes for the frequencies:
0Hz, 50Hz, 100Hz, 150Hz, 200Hz, ... , 450Hz
so If I have a single oscillator of 250Hz in first case I 'll get whatever and in second the prices {0, 0, 0, 0, 1, 0,...,0}
And if I have an oscillator of 270Hz I'll get prices that trying to give me 250Hz by adding the oscillators of the 0, 50, 100, 150 Hz,...,etc with magnitudes and phases that will result a sine 270Hz wave approximately?So the only thing I have to do is to make "big" block size so I get more analytically frequency spectrum?
Is there any way that I get the exact sine frequencies i need, no matter the block size?And one last question. If a frequency needs to be in complete deference phase (180o), it's magnitude shouldn't be negative number? How I can tell using the above type? Does the above method (rfft~) take into account this detail or it uses other system of measuring?
-
E01.spectrum.pd should be in Help Browser -> Pure Data -> 3.audio.examples.
It's going to require more than just a bigger block size, but yes, you should probably use 512, maybe more depending on your needs. To take care of the bleed you get when have a frequency between bins, you'll need to use a window. The window essentially acts as a filter, and you'll want one that has a low stop band, which typically has the trade-off of having a wide pass band. The pass band contains the energy near the bin, while the stop band contains energy bleeding from other bins. No window is the same as a rectangular window, which has a very narrow pass band and a high stop band. This makes for an ugly visualization. The Blackman-Harris one is pretty good for this. You can use [windowing/blackman~] to generate. You can get slightly better results with a Kaiser window, but calculating the normalization factor is a little trickier.
You'll have to cater your normalization amount to the window you choose. katjav posted the formula for it here:
http://puredata.hurleur.com/viewtopic.php?pid=22657#p22657
As for the magnitude, it will never be negative. There's a reason why in mathematical notation magnitude and absolute value look the same. If you look again at the magnitude calculation, you'll see that both the real and imaginary parts are squared. Once you square a negative number, it will be positive. The magnitude is taking the length of the vector containing the real and imaginary part, and you're not going to end up with a negative length.
-
Here's an example of making and applying a Hann window (another type of window that, like the Blackman that Maelstorm suggested or others, tapers your audio data down to zero at the beginning and end of each block) to your data block by block.
First fill a table with your window function.
[blocksize~ 512]