Running into error when using pwelch for FFT

I am trying to conduct a FFT on a wav file in order to then calculate SPL within a bandwidth (120-450hz). I am using code from another answered question and I get hung up on an error:
Error using signal.internal.spectral.welchparse>segment_info
The length of the segments cannot be greater than the length of the input signal.
Error in signal.internal.spectral.welchparse (line 34)
[L,noverlap,win] = segment_info(M,win1,noverlap1);
Error in welch (line 55)
signal.internal.spectral.welchparse(x,esttype,args{:});
Error in pwelch (line 170)
[welchOut{1:nargout}] = welch(x,funcName,inputArgs{:});
Error in snip_extractor (line 16)
[sensor_spectrum, freq] = pwelch(samples,w,NOVERLAP,NFFT,Fs);
My code follows. I am loading a 30sec wav file and then selecting a 5sec period with the file for analysis. Any help here would be most appreciated:
[y,Fs]=audioread('67649542.060120201700.wav') %Load fullfile
snips = y([163840:327680], [1]) %Extract data from period of interest
%%%Borrowed code from https://www.mathworks.com/matlabcentral/answers/636155-plotting-fft-for-audio-wav-file
NFFT = (Fs/2);
NOVERLAP = round(0.75*NFFT);
w = hanning(NFFT);
samples = length(snips)
dt = 1/Fs
t = (0:dt:(samples-1)*dt)
[sensor_spectrum, freq] = pwelch(samples,w,NOVERLAP,NFFT,Fs);
sensor_spectrum_dB = 20*log10(sensor_spectrum);
figure(1),semilogx(freq,sensor_spectrum_dB);grid
title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
xlabel('Frequency (Hz)');ylabel(my_ylabel);

4 commentaires

Jonas
Jonas le 7 Mai 2022
Modifié(e) : Jonas le 7 Mai 2022
maybe it would also be easier to use a bandpass filter or a combined low- and highpass on your time signal plus an splMeter object without additional frequency weighting (Z-weighting).
Would it? I'm very new at coding this. I have previously used prebuilt tools (e.g., PAMGuide) but I have a need now to write something specific for my code. Eventually I will run this to extract RMS dB re 1uPa between 120hz and 450hz for 30sec samples of about 300 wav files. I need to do some type of FFT in order to extract the sequency component of the signal.
Jonas
Jonas le 8 Mai 2022
is there a reason for re 1*10^-6 Pa and not 20*10^-6 Pa?
do you want to calculate one SPL value for one file in your frequency range or do you want to extract multiple SPL values over time for each file?
George
George le 27 Août 2024
Referencing to 1*10^-6 Pa is used for underwater signals.
That is SPL re 1 micro Pascal @ 1m @ 1W
Whereas yoru second reference, that is, 20 micro Pascals is used for referencing signal thresholds in air where it is generally acknowldeged that a normal average listener's threshold of hearing is 20 micro Pascals.

Connectez-vous pour commenter.

Réponses (1)

Jonas
Jonas le 7 Mai 2022
Modifié(e) : Jonas le 7 Mai 2022
[
sensor_spectrum, freq] = pwelch(samples,w,NOVERLAP,NFFT,Fs);
should be
[sensor_spectrum, freq] = pwelch(snips,w,NOVERLAP,NFFT,Fs);
since samples is only a scalar and not your data vector
the displayed error says, that your window of length fs/2 is lager than the given data vector, which is 1x1 by mistake

Catégories

En savoir plus sur Audio Processing Algorithm Design dans Centre d'aide et File Exchange

Tags

Commenté :

le 27 Août 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by