Hi I have used signal processing tool box and started working on this code. after the specgram function I have taken
sum(sg_dBpeak)
as a measure for my data filter and from there findingout the region of interest. This is very well working for test.txt but not test2.txt.
One more important criteria for the data filteration is I can ignore all the duration of the data where current is
Current = data(:,2);
flat. so to do this I have used some threshold value to filterout that data and then started appliying the specgram on that filteredout data.
Here is the below code.
clc
clear all
close all
% data = readmatrix('test.txt',"NumHeaderLines",1);
data = readmatrix('test2.txt',"NumHeaderLines",1);
Time = data(:,1);
Current = data(:,2);
Voltage = data(:,3);
samples = length(Time);
Fs = (samples-1)/(Time(end) - Time(1));
mn=min(Current);mx=max(Current);
% threshold=1.35/100;
threshold=2.6/100;
mnth=threshold*mn
mxth=threshold*mx
goodrows=find(Current(:)>mxth | Current(:)<mnth);
df_T=Time(goodrows,:);df_I=Current(goodrows,:);df_vs=Voltage(goodrows,:);
figure(1),
subplot(2,2,1),plot(Time,Current,'r-'),subplot(2,2,2),plot(df_T,df_I,'b');
subplot(2,2,3),plot(Time,Voltage,'r-'),subplot(2,2,4),plot(df_T,df_vs,'b');
% NB : decim = 1 will do nothing (output = input)
% decim = 5;
% if decim>1
% Current_decim = decimate(df_I,decim);
% Voltage_decim = decimate(df_vs,decim);
% Fs = Fs/decim;
% end
samples = length(df_vs);
% Time_decim = (0:samples-1)*1/Fs;
% specgramdemo(Voltage,Fs)
% SPECTROGRAM STFT ANALYSIS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FFT parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 100; %
Overlap = 0.95;
w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.
% spectrogram dB scale
spectrogram_dB_scale = 80; % dB range scale (means , the lowest displayed level is XX dB below the max level)
[sg,fsg,tsg] = specgram(df_vs,NFFT,Fs,hanning(NFFT),floor(NFFT*Overlap));
% FFT normalisation and conversion amplitude from linear to dB (peak)
sg_dBpeak = 20*log10(abs(sg))+20*log10(2/length(fsg)); % NB : X=fft(x.*hanning(N))*4/N; % hanning only
% saturation of the dB range :
% saturation_dB = 60; % dB range scale (means , the lowest displayed level is XX dB below the max level)
min_disp_dB = round(max(max(sg_dBpeak))) - spectrogram_dB_scale;
sg_dBpeak(sg_dBpeak<min_disp_dB) = min_disp_dB;
kk=sum(sg_dBpeak);
% plots spectrogram
figure(2);
ind = find(fsg>1);
fsg = fsg(ind);
sg_dBpeak = sg_dBpeak(ind,:);
% imagesc(tsg,fsg,sg_dBpeak);colormap('jet');
subplot(3,1,1),plot(tsg,interp1(df_T,df_vs,tsg))
subplot(3,1,2),imagesc(tsg,fsg,sg_dBpeak);colormap('jet');
axis('xy');grid
title(['Spectrogram / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(fsg(2)-fsg(1)) ' Hz ']);
xlabel('Time (s)');ylabel('Frequency (Hz)');
subplot(3,1,3),plot(tsg,kk);
% kk=sum(sg_dBpeak);
% tsg1=find(kk>500);
tsg1=find(kk>750);
tmax=max(df_T)+0.5e-3
tmin=min(tsg(tsg1))-0.5e-3
T_final=Time(find(Time(:)>tmin & Time(:)<tmax));
I_final=Current(find(Time(:)>tmin & Time(:)<tmax));
U_final=Voltage(find(Time(:)>tmin & Time(:)<tmax));
figure(3);
subplot(2,1,1),plot(Time,Voltage,'b',T_final,U_final,'r')
subplot(2,1,2),plot(Time,Current,'b',T_final,I_final,'r')
One more thing that I have observed is with the myspecgram function (function defined by you to get out of signal proceesing tool box) the
sum(sg_dBpak)
is turning out to be all -ve values.
Request you to help me from here.
Thanks & Regards
Sriramakrishna Turaga










