Andrew T
Andrew T

Reputation: 803

MATLAB Code Clarification

I recently visited this page in order to determine the frequency from signal data in MATLAB:

Determine frequency from signal data in MATLAB

And in this page, an answerer responded with the following code:

[maxValue,indexMax] = max(abs(fft(signal-mean(signal)))); 

From what I can see, a Fast Fourier Transform is taken on a signal named signal, its magnitude is kept by using 'abs', and the max value is computed. The max value will be in maxValue, and the indexMax will contain the position of the maxValue. However, can someone explain what is meant by signal-mean, and what the purpose of it?

Upvotes: 0

Views: 85

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112759

As stated in vsoftco's answer, signal-mean(signal) subtracts the mean of the signal.

However, the key point is: why is this is done? If you don't subtract the mean, it's very likely that the maximum peak in the FFT appears at frequency 0 (DC component). But you don't want to detect that as the "frequency" of your signal, even if it truly is the highest spectral component. So you remove that zero-frequency component by subtracting the mean. That way, the max operation will detect the maximum non-zero frequency component, which is probably what you want.

Upvotes: 3

vsoftco
vsoftco

Reputation: 56577

It basically normalize the vector signal so it has mean zero (subtracts the mean from signal). So signal - mean(signal) looks like signal except that is shifted on the y axis so it has a zero mean. Hope it is clear.

In the example you posted in the link, the mean of the signal is around -2, so by subtracting the mean you end up with a the signal shifted up around the y=0 axis.

Upvotes: 3

Related Questions