Use FFT to Read and Display Frequency Bands
Joseph Gray
Lessons
Lesson Info
Use FFT to Read and Display Frequency Bands
All right, Segment three eso. Now we're going to get into something a little more complicated. We're gonna use FFT, which is fast for your transforms. We're going to use that to find the frequencies in that raw audio data, because what you're seeing is just the the actual wave form itself. But it doesn't tell us things, like Is something low pitch or something High pitch or kind of in the mid range and so fast for your transforms are a bunch of really complicated math that fortunately, fortunately, the minimum library wraps up for us nicely, uh, so that you don't have to, you know, do all this really complicated math yourself. Um, I'd say, you know, you could go into Wikipedia and look at the Fourier. Uh, Siri's article is pretty clear. You could also just look up fast. Fourier transforms on Wikipedia, But that is really complicated math. I don't understand, but, um well, hop into this for your transform Wikipedia article because it does has some visuals that will help us understand. S...
o essentially it's it breaks away form into its constituent frequencies, is what they're saying. They're and they show somewhere here some pictures basically describing all this, huh? I was only Yeah, There were some nice pictures here. Well, this kind of thing shows you anyways, you kind of analyze this stuff, but fortunately, the library will do a lot of this hard core math for us. Okay, So going back into the audio tab inside Processing. Um, we've already set up the analysis import for the minimum library, and that's that contains the stuff for the FFT for the frequency tracking stuff. And we'll also it also contains this stuff for the beat tracking stuff that will do later. So we need to declare a few things and we're going to declare them right below where we set up our audio and put kind of basic things. First thing we wanna declare is of class fft just an object called FFT Lower case, and then we're gonna create an imager, which is FFT averages. Which averages is kind of doesn't make a lot of sense if you're not familiar with how these things work. But essentially what those air, the number of frequency band like ranges, frequency ranges that we have available to us that get data out of So it'll be like, you know, 60 hertz to 120 hertz, you know? And that's like one of the averages is that determined by the men and library or by processing, that is determined by how we set up our f f t object with minimal Got it. Okay, um, and will be assigning this a number that minimum will actually give us leader, um, and then blow that. We also want to create another float array, and that's going to be just called frequencies. And that will store the volumes of each of our frequency bands. Then inside, set up audio bringing go, um, fft bring a science or in Stan, she ate it as a new FFT class object of the FFT class and just pass into it similarly to, you know, audio end. But just with two arguments buffer size and sample rate just telling it what are the numbers that we're working with here? And then we're gonna call this other thing, which is maybe perhaps the most mysterious part of this whole process. And, you know, just really, if you want to get into this, look inside the documentation in the minimum library. Um, it's long and complex, but if you read through it, it will make more sense. FFT don't log averages, and normally, if you're just doing FFT just straight straight off, it's going to give you frequency data. But it's not going to really be mapped to anything that the way humans understand sound. We think we hear pitches and we hear pitches at different frequencies as being the same kind of tone, like the beginning of a knock tive sounds similar to us as the next frequency doubled up from it. So if you take a frequency and you multiply it by two, that's called an octave, essentially. So if you start off with like, 1000 Hertz than the next octave up is 2000 hertz. And so what this is doing is it's breaking apart our frequency bands into octaves like that because we understand it better. Um, and like I said, you don't really need to understand exactly all of this stuff, but what we're doing is we're starting off with our lowest. Our largest are starting band with its 60 Hertz whip, and then we're going to divide each octave into three parts, and that's what those these two numbers were passing in. There are, um and I'm choosing these two numbers because those are also happened to be the same numbers at the beat tracking class that will use later uses to do fast for your transforms. It has its own fast. Fourier transforms. It does inside itself, and these are the numbers that uses that way. When we're visualizing our data, we can get a better idea of where to look when we want to find specific frequency ranges to track beats. And that's more advanced stuff. And that's in the tip sheet that comes with this class. We won't get into it during the video, but if you purchase the class that there is a bit more discussion on that how to really take advantage of this? Um, but Long In the short of it, all you need to know is we're just finding frequency data refining volumes per frequency range. Then we're going to use this FFT averages, and we're gonna assign it. Fft dot have a V g size average size, and that's the total number of averages that are available to us, meaning the total number of um, frequency ranges discrete frequency ranges that we can pull that we can ask for volume data from. And that's calculated after this is called because it uses thes numbers to figure out how many band with how maney frequency bands we have available to us, and we'll use those later. We'll loop through those later to draw the frequencies to the screen. And then finally, we're just going to and Stan, she ate the frequencies, see free when sees and cools new floats, an array of type float, and it's going to be a length of the FFT averages. So the total number of frequency bands we have available to us then moving down into get audio data we need to actually look for this information every frame and get audio data is already being called inside draw. So we might as well add this stuff in here and f f t dot Forward is another bit of a mysterious thing, but not too bad. All it's doing is we're taking our audio in the mix and passing it into the FFT object. The most current samples of that air in audio audio end up mix. We're passing to the FFT object for it to new calculations with and then we're gonna just do a four loop. And I'm just gonna kind of copy this for leap up here, paste it down here, close it out, and then we're gonna exchange I two j and then inside here was going to go free seas and then the J index of free frequencies array. And we're gonna sign it. Fft what's returned to us by the fft dot Get, um, a V G, which is to get the average, and we're gonna grab a specific one, which is gonna be the same index J as the frequencies index that were passing Teoh or they're assigning to. Okay, so that said, go back into the main audiovisual, Isar 10. And we're going to take this whole volume wave bit that we already had created because it does do this way former rendering for us, this kind of mirrored way of former rendering, which, you know, there's a lot of different ways you can render way forms. This is just a way on. We're gonna change. We're going to just copy that whole thing and then paste it right below itself. And then we're gonna change the function name too frequent si wave. And that's just gonna draw away form that shows us our frequencies with the low being on the right or on the left side and the high end being on the right side in mid tones, being in the middle. Um, and a few changes we're gonna need to make to this one is that we're not going to be cycling through with because there's actually unless fft averages, then we have X coordinates on the screen. Us will be drawing fewer points of the screen than there are expositions and then connecting those together with mine, um, which is essentially what the other one did as well. It's just that it was doing it like on a per pixel basis. And so what that means is that our percent is no longer going to be about the width, but it's going to be about fft averages. And then when we want to find our index, we actually multiply that percentage of how far we are through it against FFT averages to find our index, Um X then is no longer just simply the same thing as I or directly correlating the eye But instead it's going toe correlate to the percentage that we are through. Um and then multiply that by the total width to give us a placement on the screen and then in sight here because of the way the FFT object works, it returns positive values always. So we don't need to put abs around it. We don't need the absolute vote value. Um, but we do need to replace volumes with frequencies and then we're still gonna multiply that against the amplitude that were passing in appear and then protects X y it and critics x y times negative One can just stay there. Okay? And so then going back up into the main draw loop, we're going to go up here towards his volume wave, and we're just gonna replace that with frequency wave, and then we're going to try running it. Oh, I got it out of bounds, right. Come on. All right, let me go back here and make sure I got everything right. All right. So, uh, yeah, I o That's because buffer size is totally different things in the FFT averages. We actually need the loop through the FFT averages here. Okay, Now try it. Okay, so that's it. Gave your own. Remind me again which part of the line represents which of the tones. Okay, so the one on the left is the low end. So can you play something relatively low on something higher Pitch just tryingto like do maybe octaves. Uh ah. So you can see it in its pretty noisy from like what we are. The human brain turns into a frequency as opposed with the computer. Turns into the frequency are kind of two different things, but it's It's definitely a good rough approximation. And especially as your visuals get more complex and that sort of thing, the human brain also becomes less discerning as long as it can get some kind of close approximation where it's like, OK, I heard a sound and I saw something happen and I heard this sound and that happened, and I heard another sounded and something else happened then that's usually good enough for like, kind of a passive observer is not sitting there trying to analyze. Is this really that accurate? I mean to us, we're trying to get accurate, and so it could be Why isn't it exactly it's just It's the It's the nature of audio frequency analysis. So anyways, um, that's it for that section. What we just did is set up those FFT functions, parts, the audio, every frame into frequency bands and then drew those frequency bands as away form on the screen. Any question of that? Yeah, we had a question. Can you remind us what the import is for the FFT? Is it just the minimum? It'll be minimum, and then it'll be minimum dot analysis. So let's go back in there and look at the top here. When we first imported this, it wanted to import everything we released did on the version of processing. I'm running, but we want to explicitly, like, say, we only want the core Menem library and then it's analysis functions, God, isis classes and methods. And then can you Can you go over again exactly what the forward does? Because I get really confused about how forward works. Okay, all forward is is it's it's telling the FFT object. Hey, here's the most current of sample of samples of audio data that I have like or that the audio input has, and then it's just passing it into the FFT object so that it has something to work with. Okay, so that it has the latest data to do. It's math. I see. Okay, it's just it seems like a funny function. Name that right. And again, this this vocabulary goes back if into If you really understand how 50 works, which I don't necessarily right. But I know that this vocabulary comes from that sort of. You move chunks of data forward, toe, analyze. And you know, for all intents and purposes, we just need to understand that as giving that fft object freshest audio data work. Um, we have a question about me. I'll just read it out in FFT draw function. The percent variable and the index variable should come out to the same thing. Since the percent is divided by the same thing as what the index's multiplied by just kind of complicated. Yes. No, you're right. It's true. I think that I had removed that other versions of this code. I think I just put that in there. But you're right. You should be able to just comment that out, and I in here should play. So can you explain that more what you just did. Um, I wasn't sure I understood the question. Right. Okay, So basically, this is a redundant piece of code that gets kind. It's kind of becomes legacy That should just be removed because you're taking your total number of you're taking your percentage of the way through, and then multiplying it by the total number of our is to give us back the index. But because we got that percentage by taking I and dividing it by a little write, it becomes the same thing That makes good call. All right. And then can any of we had a question about Can any of the set up code be done once instead of by frame, like setting the FFT parameter 63 for example? Um should be. So if you go and look up inside of set up audio where we're setting up all this stuff and then set up audio if you recall from the last segment is only called once at the beginning. Endurance. OK? Yeah. So it is just being called once. Okay, good to get clarification on that
Ratings and Reviews
Student Work
Related Classes
Mixed Media Art