Running into error when using pwelch for FFT

37 views (last 30 days)
Benjamin Colbert
Benjamin Colbert on 7 May 2022
Commented: Jonas on 8 May 2022
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)
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
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);
title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
xlabel('Frequency (Hz)');ylabel(my_ylabel);
Jonas on 8 May 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?

Sign in to comment.

Answers (1)

Jonas on 7 May 2022
Edited: Jonas on 7 May 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


Community Treasure Hunt

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

Start Hunting!

Translated by