Running into error when using pwelch for FFT

31 views (last 30 days)
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);
  3 Comments
Benjamin Colbert
Benjamin Colbert on 7 May 2022
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 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
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

Tags

Community Treasure Hunt

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

Start Hunting!