Clear Filters
Clear Filters

I am trying to perform Short-Time Fourier Transform (STFT) on two sets of 3D data

12 views (last 30 days)
The link to the dataset is:
I have run the provided code, but I keep encountering errors. Could someone please explain these errors to me and suggest how I can resolve them?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data1;%data1 file
L = length(data1);
Fs = 800;
t=[0 : 1/Fs : L/Fs - 1/Fs].'; %time coloumn
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax=1; % axis, x=1, y=2, z=3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
d=(1/2048)*9.81*data1(:,ax); %acceleration reading
%d=data1fulldata1fft(:,axv); %time reading
doff=repmat(-mean(d),[L 1]); %active only for z axis g offset
d=d+doff; %active only for z axis g offset
%set(gca,'fontsize',12)
FigHandle = figure('Position', [100, 100, 1280, 720]);
subplot(2,2,1)
plot(t,d);
title(['Time domain of recorded vibration, axis = ' num2str(ax)],'FontSize',12)
xlabel('Time (s)','FontSize',14);
ylabel('Acceleration (m/s^2)','FontSize',14);
axis tight;
grid
Fs=800; %sampling frequency
T=1/Fs; %sampling time
tt=(0:L-1)*T; %time vector
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(d,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
%Phase Angle
%phi = unwrap(angle(Y(1:NFFT/2+1)));
subplot(2,2,2)
plot(f,2*abs(Y(1:NFFT/2+1)));
%title('Single-Sided Amplitude Spectrum of y(t)')
title(['FFT of recorded vibration, axis = ' num2str(ax)],'FontSize',12)
xlabel('Frequency (Hz)','FontSize',14)
%ylabel('|Y(f)|')
ylabel('Acceleration (m/s^2)','FontSize',14)
xlim([0 0.5*max(f)]);
grid
%%%STFT%%%
fs=Fs;
%colormap('default');
x=d;
%figure(3)
%plot(t,x,'k');
%xlabel('Time (sec)','FontSize',14);
%ylabel('\it{x(t)}','FontSize',14);
%axis tight;
%grid
%for nfft = [128 64 32] % Window size
for nfft = [128] % window size
% Compute spectrogram in Hamming window, 50% overlap
% [B,f,t] = spectrogram(x,nfft,[],nfft,fs);
% figure(4);
% mesh(t,f,abs(B));
% xlabel('Time (sec)','FontSize',14);
% ylabel('Frequency (Hz)','FontSize',14);
% axis tight;
% colorbar;
% colormap('default'); caxis([0 100]);
% title(['STFT, Window size = ' num2str(nfft) ' Window type= Hamming' ]);
%figure(5);
% contour(t,f,abs(B));
% xlabel('Time (sec)','FontSize',14);
% ylabel('Frequency (Hz)','FontSize',14);
% axis([0 max(t) 10 200]);
% grid;
% colorbar;
% colormap('default'); caxis([0 100]);
% title(['STFT, Window size = ' num2str(nfft) ' Window type= Hamming']);
% Compute spectrogram in Chebychev window, 50% overlap
w = window(@chebwin,nfft);
[B,f,t] = spectrogram(x,w,[],nfft,fs);
subplot(2,2,3)
contour3(t,f,abs(B),128);
xlabel('Time (s)','FontSize',14);
ylabel('Frequency (Hz)','FontSize',14);
zlabel('Spectrogram amplitude','FontSize',14);
ylim([0 0.5*max(f)]);
colorbar;
colormap('default'); %caxis([0 100]);
title(['STFT, Chebyshev Window size = ' num2str(nfft) ', axis = ' num2str(ax)],'FontSize',12);
view([45 45 45]);
subplot(2,2,4)
contour(t,f,abs(B),32);
xlabel('Time (s)','FontSize',14);
ylabel('Frequency (Hz)','FontSize',14);
axis([0 max(t) 0 0.5*max(f)]);
grid;
colorbar;
colormap('default'); %caxis([0 100]);
title(['STFT, Chebyshev Window size = ' num2str(nfft) ', axis = ' num2str(ax)],'FontSize',12);
end
These are the errors:
Error using pwelch
Expected x to be finite.
Error in signal.internal.spectral.welchparse>parse_inputs (line 111)
validateattributes(x2,{'single','double'}, {'finite','nonnan'},'pwelch','x')
Error in signal.internal.spectral.welchparse (line 31)
parse_inputs(x1,esttype,varargin{:});
Error in pspectrogram (line 30)
[xw,nx,~,yw,ny,win,~,~,noverlap,~,~,options] = signal.internal.spectral.welchparse(x,esttype,inpArgs{:});
Error in spectrogram (line 191)
[varargout{1:nargout}] = pspectrogram({x},'spect',inpArgs{:});
Error in shorttimefouriertransform (line 97)
[B,f,t] = spectrogram(x,w,[],nfft,fs);
Could someone please provide an explanation for these error to me please and nd guide me on how to address them?
My main task is to determine the health status of the wheels. Thank you.
  5 Comments
Star Strider
Star Strider on 1 Oct 2023
Use the paperclip ‘Attachments’ icon button in the top toolstrip to upolad the file here. If the file extension isn’t permitted (for example .dat files), use the zip function to create a .zip file for it and then attach (upload) that file here.
I generally do not bother to access files from offline sites.

Sign in to comment.

Answers (1)

dpb
dpb on 1 Oct 2023
Edited: dpb on 1 Oct 2023
Error using pwelch
Expected x to be finite.
The problem is your input data have either NaN or Inf values somewhere in it ... NaN is the more likely that some of the values were either missing or nonnumeric that you tried to read.
Fix the reading of the file to remove those problems first.
The plotting routines in MATLAB silently ignore non-finite values so you won't see or get a warning when you first plot the data; it's not until you try to do the spectral analysis the problem shows up.
Look at the input file first to see what is irregular...
What does
all(isfinite(data1),'all')
return at the command line? Undoubtedly it will be "0" or False...
find(~isfinite(data1))
will locate the places that aren't; you can then see where in the file the problem lies...
  2 Comments
dpb
dpb on 1 Oct 2023
Edited: dpb on 1 Oct 2023
unzip("Test Run 2 and 3.zip")
dir
. .. Test Run 2 and 3. 7z Test Run 2 and 3.zip
So, what do these uninformative file types consist of??? "Help us help you!!!!"
delete("Test Run 2 and 3.zip")
d=dir;
d=d(~[d.isdir])
d = struct with fields:
name: 'Test Run 2 and 3. 7z' folder: '/users/mss.system.kef5Ur' date: '01-Oct-2023 02:38:08' bytes: 1922442 isdir: 0 datenum: 7.3916e+05
R23=readmatrix(d.name,'FileType','text');
whos R23
Name Size Bytes Class Attributes R23 14734x10 2357440 double complex
R23(1:5,:)
ans = 5×10
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
any(isfinite(R23),'all')
ans = logical
1
So, that explains your problem; at least the beginning of the file wasn't read as valid numeric data...so, where are there numbers?
[r,c]=find(isfinite(R23),1)
r = 962
c = 1
It's almost a 1000 rows into the file before it is numeric -- at this point I've gotta' go do other things...
OK, back for a few...
file=readlines(d.name); % look at the raw file content
[file(1:10);file(960:970)] % first few lines then around where turned numeric
ans = 21×1 string array
"7z¼¯'□ □!fܬøT□ r t,M­ã□»Á□] □□→'C□x&Zí¥%}Y¿□J□L□ô¢À□|Î□□9?Y¿r□.HkZÑI|Åðܬ>BçQvnÅ0ú«î¢Þb¿ìvdT□" "□□zÄ9ÿ°□Wº¥0¾□□êa□<□□□" "□¿æ«□>aò¦ö□;¨¶ï8!□4è□$þãâ±Î□;=MóÿÿL□<9G□#□&47¼)□à□Àj□í>(0+ÖÚû□~□âpWÊÝ□□□.ÐÙ□<" "Ä£0□rüÄ□ó0þÒÉ□«F□w÷□□□\" " 8,Ëí□NS°>­¶□µZ#û9Õ□nã¯Æã□´Ít□¢!hâs□Ĺ¾&□=»ý□□­Õg®¬e□h­C" "'ì□§·ë□□q□□B\ò(□Ë{□A□S[□□□□□%pÇ>¤□6□□□ÁL;-¡ ¦©+□□□□Ða­Ä□¶qº□□IYo\□þê§b□·ÿ6u8¾Hj·èÈ□□·¬Ê□3□²□HÃѺ÷8=»åæÔCT*¶ÔÞ§f5J□I□□□" "÷□Éù□ɪ□z<ÎQ□ü□ëf(□?□¹v□øú³□¼ÌU□V□óÀY§□\F!<C,;□□iKn□$□□¥üC□*7»Ø□□Èzï»!H¦¢êù□I□o(u¡=□□èÉåÐóòµË□ EõYî□"J□+ÃÌÄN□□é;□□kÇ□ÁÃe□Ã}□"ï□ÿRwå□e□R□;□|ê□" "□□" "±ÖÊÚüv¡%Ü5□□eâ#□□f£□3□□4gÛ¾□□□>dzç7ÕÞs árw□Ëkë8□□®»X]n[¿è]□¾%×□Vî%¦þ2bïݬ­Ð□é¬□ë¶vÄ□□u□□□ãRè□ܧZQ□□v>ül_ZäpÕ0□6æa□Ý¡¯ýâ|ð→□À¥ýÊK®Kú,?UºµË□=I□□o=óá8□□□ºx4Zyÿ□F%ê□¡_ã□®[W¦°¬-g□²Ë□□;wóH`]□&¤WÁr□Ó□¼□âðÓ©ABle%¶□□□t□O□ü" "□□□uL¥□?vÜýv□□□□Ú□¸ë3\□­·(ݧ¿MpïLgÖî□ÉD□|£Z□□¬¡@\□□²ºÛx? □#üµ{m°□ìöcb£B®□ÿ«¹hølUù□åW°□□à□□+\j;7×□;Ä $□a&m□;□gs¯¼Øó}×□□Mc□³□□×dæÝAÏ>□è□¢-gqN□,+F□□g´|~Èà¯Ôv□Gæ_□" "n□Xºùe□TÖZ□EÖN□tNK&Çëuî¥{\ç□ÐÞ□A□²¤→U ·>,¶³D÷□□hé#u□&äH÷þQ\fö]:g¢6¤²P□x|1féi?²ÑÈCIìºJØ.jBì³ú□7¡UæÓiPüb□;*ÐÍCJ?Õ□ê+ù×X²□uh>□!Fb<ÈÆ'l6VÝÑ eV□ü/A□□□*□%O«S" "□□­¯□z}5□·□ÿÖâ□□í□$å/6´µW(û<bÇ□_tvbô□?$□W]ºò½□ZÛÅ¡□@?□□èöQ*□±û¿Ë6□ÔJÅî□ä:□Ör}L4Ûû#þ§ê8Éõqì°S□dÙ→Jþ□Ñ□Spï□?Bü□=□UÝÞz□öT□□·ö{~□□□X□□□·_éËVFµ¯ç­x<□dq\@Z@5;□c□□□SÝ^□jÙ³P~□¾□w□□¾7□îoÙ§æ□Ee©□ø□□¦ÿC$ÙVûÖ□ò/f»$□ □d®KìP;·J²□1s□<ÒKëï,¡>Û" "õ&éBêÁ¶Ï!ÞÓ□6M/z□èI□ï□áF□urìÆ□ߢã□â8□□□□ãvurc¡□□□È□,Öë¾É□ÈZqúß߬□Â?Ò!¶□Õ□±ÇSøý7|¨GcF\ßî¹,%□:Ý□³ÐVDZ®ý+□I□□ÖÚë86U□□tÉS [BzZ³□B·□fx¥L{c¾ÁTëV»:È\ãe□" "Ò □□□çñÛÉ ª°³□¨a□ÏkHÔçä□@□□□ï□y□D" "®óN`d;□□ºwö'LKTr.□ý«ÀE%□□□î/¼|□­jÂ"□□□wÄv8¤_ù/Oaº7Ñ □□□{□>□ü,05□□<ÓÏí□Á□Ü□" "□□¤□6ñ□5□GÖÿÓͳ□ør+RJÑ□Ü"×ÅsßÄ®¶ä□□õ0:t□µ¥□□úë>:CßÑ→□?$□ì*VgB¸□□□□¹O)ù,ɹ□bbúðqB2Ú□½×¡ÎÐ□,¿□Þ□íÕ øÒ□;□£□*□gëä¹z□□°xÌ□¨¼Ûê«□óS□å5Ò□zCÕDðÒ"ûÏïǪÇ□□□□□□¹¹□ðÑA)□gÜe□□□d□□×M©□□G□□H□Ôe□²¨□é&×□òs□5gä□û̾ɮÉòÀ□²□ä□5àqV)□Æ□çj*¨□U□Ç&jãÅtBÐQF□ªúm¼□ç□³Ý□□□□ÄͲò□□□ÄÔP□□K8~i□□Î$éUɬ□36iq:½n4Y=qêu□Íüà□<D□t□®□}M@□¤Ù:*Ay4□□½□¼®Î~沪□/^<_HU|W¹Zê¨Ã□º%®ÅiÄÁÒ□h□□ºþ□□´Å?nN□□C□ò□□´¼8t□g□□!êÎ□îS□□Æé□□¹1□É(Åó3Éo§ó□)ez□Éá&+□£□¯¼Þ□□ù□?d□□3□»V□ÒcßJ□□□$S,nå□+Ó¬)□7Ïz[V□ò" "çI,□Õ" "é|÷»□□e&<¼!Ô{□ýKÜ□□ø+}÷□,□|□7□ êv□¿□é}pJ¿§□`ðU#Ê□#□©□z□□â"S­îë -ÙF-4(ãTZE□→x□Ï□èS#Ä□â□¾□□i¨□á/½ûÊA□□□º¾□í³□□□□Bûcå)□æ□¢MÏ□□cÇlÏ□nm□8ó□□FvO*□þ□+Í□□□|Æ©J¡Ê"r□b□" "j¨³□s=e[□ñ÷½Y4£Ô~,□□¥¡□´□{□Ú" "½□Zõ<i<l□¸$Á]U□®" "□fz%Ll!□(ÊÁW­□Bt□Ój□M9□¹»m□ÈõP□g{Wص□□□"□y□h$CwN¾Ë □ÁT□Pç4«□<□"_Ô5¨1ÊY³×½òì-ä¢□□Þ²¡ÆÝ"
Well, that's informative if not helpful...turns out it isn't a text file at all -- but we don't know what it is...
ADDENDUM
No chance saved a .mat file with funky name, is there????
load(d.name,'-mat');
Error using load
Unable to read MAT-file /users/mss.system.kef5Ur/Test Run 2 and 3. 7z. Not a binary MAT-file. Try load -ASCII to read as text.
Nope...
Sam Chak
Sam Chak on 2 Oct 2023
Hi @dpb,
It seems like there might have been a small oversight in the file attachment process. Initially, it appears that the CSV file exceeded the allowable size limit at 9 MB, so @Oluwasegun (OP) compressed it using 7-zip, resulting in a .7z format. However, it seems there might have been a slight misunderstanding as the .7z format is not supported here. Subsequently, the OP compressed the .7z file into a .zip format without realizing it, which led to some unexpected gibberish characters when it was unzipped.
I have compressed the CSV in a .zip format, and it should be readable upon unzipping. Here is a screenshot for your reference.
unzip("Test_Run_2_and_3_Sam.zip")
dir
. .. Test Run 2 and 3.CSV Test_Run_2_and_3_Sam.zip

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!