plot fails with -Inf
Afficher commentaires plus anciens
I need to plot a fft of an audio file. It appears that if any of the fft bins are zero, the conversion to dB produces an -Inf. Then the plot function does draw the figure but does not plot any data.
Is there an efficent way to trap the 0 value bins in the conversion to dB? For my work -Inf PSD would be equiv to zero.
The test audio file was a code generated audio file, 100Hz Sine for 10 seconds. Since only one fft bin will have a value, all the other bins are zero.
Thanks.
%Code Fragment:
[yS, Fs] = audioread(PathFN,'double');
L = length(yS);
Y = fft(yS,L);
% Version One
Px = Y.*conj(Y); % PSD Power of each freq components
Px = Px(1:L/2);
Px = 10*log10(Px); % Convert to dB
fVals = Fs * (0:(L/2)-1)/L;
plot(fVals, Px); % Plot
3 commentaires
Peng Li
le 23 Mar 2020
log(0) = -Inf so I don't personally understand why you think -Inf PSD should be zero.
If you want to show the log(0) part in figure, you can add a small offset to Px such as Px = 10*log10(Px + eps).
Jonathan
le 23 Mar 2020
Peng Li
le 23 Mar 2020
did you try my solution? If you add a small number like eps to Px, there won't be any -inf.
Réponses (1)
Stijn Haenen
le 23 Mar 2020
0 votes
you can use Px(isinf(abs(Px)))=0; to set all -Inf values to 0
3 commentaires
Jonathan
le 23 Mar 2020
Stijn Haenen
le 23 Mar 2020
Modifié(e) : Stijn Haenen
le 23 Mar 2020
did you place it after the convertion to dB?
like this:
Px = Y.*conj(Y); % PSD Power of each freq components
Px = Px(1:L/2);
Px = 10*log10(Px); % Convert to dB
Px(isinf(abs(Px)))=0;
fVals = Fs * (0:(L/2)-1)/L;
plot(fVals, Px); % Plot
Does your Px also contains non real values?
Jonathan
le 23 Mar 2020
Catégories
En savoir plus sur Parametric Modeling dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!