Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by miketucker · Oct 10, 2011 at 05:58 PM · audiofilterpass

Audio Analysis: isolating frequency values

I'm looking for the amplitude of frequencies 570-650. how could i obtain those from GetOutputData?

I'm trying to read certain frequencies from the audio output data, but no idea how many samples this requires or which FFTWindow to use?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by aldonaletto · Oct 10, 2011 at 07:02 PM

You must use GetSpectrumData to analyse a frequency range: allocate a float array with a power-of-two size and pass it to GetSpectrumData. The size (lets call it Q ) defines the frequency resolution: each element shows the relative amplitude (0..1) of the frequency N * 24000/Q Hertz (where N is the element index and 24000 is half the audio sampling rate in a typical PC - read AudioSettings.outputSampleRate for the correct sampling rate). With a 1024 array, for instance, you will have a resolution of 23.4 hertz (each element refers to a frequency 23.4 Hz higher than the previous one). You can use 2048 samples as well, which gives a resolution of 11.7 Hz, but the time taken to get the spectrum may lower your frame rate. Use the BlackmanHarris window - it gives more precise results and isn't so expensive.

The function BandVol below gets the spectrum and returns the average intensity of the range of frequencies specified. You can place this code in your script and attach it to the AudioSource. Play the sound and call BandVol to get the frequency band volume. You can call the function during several Update cycles and average the results, for instance:

 private var freqData: float[];
 private var nSamples: int = 1024;
 private var fMax: float;
  
 function BandVol(fLow:float, fHigh:float): float {
 
     fLow = Mathf.Clamp(fLow, 20, fMax); // limit low...
     fHigh = Mathf.Clamp(fHigh, fLow, fMax); // and high frequencies
     // get spectrum
     audio.GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); 
     var n1: int = Mathf.Floor(fLow * nSamples / fMax);
     var n2: int = Mathf.Floor(fHigh * nSamples / fMax);
     var sum: float = 0;
     // average the volumes of frequencies fLow to fHigh
     for (var i=n1; i<=n2; i++){
         sum += freqData[i];
     }
     return sum / (n2 - n1 + 1);
 }

 function Start() {
        freqData = new float[nSamples];
     fMax = AudioSettings.outputSampleRate/2;
 }


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image miketucker · Oct 11, 2011 at 10:38 AM 0
Share

wow, beautiful. thank you!

avatar image se7en · Jan 22, 2014 at 05:55 AM 0
Share

I had a similar question - This works perfectly thx

avatar image MarkV8 · Jun 12, 2016 at 03:33 PM 0
Share

Very useful! Thanks!

avatar image GuardianArchangelJohn · Nov 18, 2017 at 02:59 AM 0
Share

Thank you for this thorough response. I was trying to understand the correlation between samples and frequencies. I knew from visually looking at waveforms that the lower ones were lower Hz. I need to know how they were partitioned. The docs for Unity don't explain that much detail.

I'm still trying to wrap my $$anonymous$$d around FFTs and Laplace transforms. Both for audio and engineering.

avatar image Bunny83 GuardianArchangelJohn · Nov 18, 2017 at 04:49 AM 0
Share

The samples you get from GetOutputData is just a subsection of the time series data that makes up the wave form. Looking at the samples directly doesn't help much. All you can immediately see from looking at the samples is the average volume in a certain area.


To actually do frequency analysis you have to apply the discrete fourier transform to a section of your time series data. The resulting values are essentially the frequencies the original signal was made up inside the given subsection.


You could do a discrete fourier transform basically for arbitary frequencies. However this is computationally very expensive. The advantage of the fast fourier transform is that it uses many "shortcuts". This is possible because we only look at n frequencies equally spaced. So in a normal FFT of say 1024 values you get the frequencies from 0Hz up to the sample frequency (which may be 44kHz or 48kHz). So the lowest value usually is 0Hz which is basically a fix offset that never changes. Each "frequency bin" / frequency value represents one particular frequency. That is i * sampleFrequency / numberOfBins where i is the index. So at 44kHz you get the frequencies: 0Hz, 42.97Hz, 85.94Hz, 128.91Hz, 171.88Hz, ...., 44kHz


The guy in this video actually has a great approach explaining how the FFT works even though he messes up a lot details and numbers in his examples (just see some comments ^^). However i think he actually does a great job at explaining what actually happens. The first 15 $$anonymous$$utes contain the important part. The rest is more about DFT vs FFT, windowing, Eulers formula, overlapping FFTs when you have a lot data / stream and some more examples.

avatar image Bunny83 GuardianArchangelJohn · Nov 18, 2017 at 04:52 AM 0
Share

ps: Over here i actually implemented the FFT algorithm in pure C# some time ago.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio Low Pass Filter Not Working 1 Answer

Unity 5: AudioLowPassfilter.cutoffFrequency not doing anything 1 Answer

OnAudioFilterRead issues on Android? 1 Answer

GC lags cause sound to stutter (buffer underrun) when using OnAudioFilterRead 0 Answers

Truncate audio runtime 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges