-3

I have FFT for signal (audio signal) code in MATLAB but it displays (plots) a graph. However I just want the frequency component to be stored in a variable but I don't know how. If the code is not applicable to do so can anyone give me code that will work in Matlab or in Java ?

Note : I am not an expert in signal processing or MATLAB.


   %% Basic Fourier Analysis
   % This example uses the Fourier transform to identify component
   % frequencies in a simple signal. 

   %%
   % Create a time vector |t| and a sinusoidal signal |x| that is a        function of |t|.
   t = 0:1/50:10-1/50;                     
   x = sin(2*pi*15*t) + sin(2*pi*20*t);

   %%
   % Plot the signal as a function of time.
   plot(t,x)

   %%
   % Compute the Fourier transform of the signal, and then compute the   magnitude
   % |m| and phase |p| of the signal.  
   y = fft(x);         
   m = abs(y);                               
   p = angle(y);                     

   %%
   % Compute the frequency vector associated with the signal |y|, which is
   % sampled in frequency space.
   f = (0:length(y)-1)*50/length(y);



   %% 
   % Plot the magnitude and phase of the signal as a function of frequency.
   % The spikes in magnitude correspond to the signal's frequency
   % components.
   subplot(2,1,1)
   plot(f,m)
   title('Magnitude')

   subplot(2,1,2)
   plot(f,rad2deg(p))
   title('Phase')

   %%
   % Compute and plot the inverse transform of $y$, which reproduces the
   % original data in $x$ up to round-off error.
   figure
   x2 = ifft(y);
   plot(t,x2)
Paul R
  • 208,748
  • 37
  • 389
  • 560
Moni T
  • 13
  • 5

1 Answers1

2

In the MATLAB code in your question the vector m contains the magnitudes of each FFT output bin. Each bin corresponds to a frequency, so this vector gives you magnitude at each bin frequency. If you are looking for the frequency of the largest peak in the spectrum then just find the max value in m and then convert the bin index of this peak value to a frequency. The relationship between bin index and frequency is given by:

f = i * Fs / N

where i in the index of the bin of interest, Fs is the sample rate, N is the FFT size, and f is the corresponding bin centre frequency. See this question for a more detailed explanation.

Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560