returned T in spectrogram()

1 view (last 30 days)
Gabriele
Gabriele on 4 Feb 2013
Hi All!
Suppose that I want to divide a signal in segments and I want to calculate the center of those segments and the respective time. Suppose that I' ve
signal = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17];
fs_audio = 0.5; % sampling frequency = 0.5 HZ
winLen_Sec_desired = 5 % length of a segment in seconds = 5 seconds
winOverlap_desired = 0.4 % percentage of overlapping segments = 40%
depending on the sampling frequency the desired parameter won't be the real ones, but it's ok...
winLenSamp = round(winLenSec_desired*fs_audio); % length of a segment in
% samples
if mod(winLenSamp,2) == 0 % check that the segment has an odd size
winLenSamp = winLenSamp+1;
end
winLenSec_Real = winLenSamp/fs_audio; % our window size will be slightly
% different from the one desired
winOverlapSamp = ceil(winLenSamp*winOverlap_desired);
winOverlap_Perc = winOverlapSamp/winLenSamp; % our overlap will be slightly
% different from the one desired
winStep = winLenSamp - winOverlapSamp; % we'll have a center each winStep
% samples
firstCenter = ceil(winLenSamp*0.5); % calculate the first center
j = 1;
i = firstCenter;
while (i + winLenSamp - firstCenter) <= length(signal) %till we exceed
% the dimension
winCenters(j) = i; %save in winCenters the center of the segment j
j= j +1; % increment the segment
i= i + winStep; % calculate the new center position
end
time = (0:length(signal)-1)/fs_audio;
winTime = time(winCenters);
I hope that till here everything is correct... Anyway I'm interested in winTime and winCenters
Now I have to use the spectrogram function in matlab to compute the STFT of the signal. In particular I use
VECTORFREQ = [150:10:400];
[S F T P] = spectrogram(signal, rectwin(winLenSamp), winOverlapSamp, VECTORFREQ, fs_audio, 'yaxis');
in order to have back the vector T containing the time instants for each segment in which the FFT is calculated.
I guess that T and winTime calculated before should be the same but they are not! Any guess?
Thanks a lot Gabriele
ps I know that to divide a signal in segments I can use the function buffer, but it doesn't return the center of the segments nor the time where each segment is centered.

Accepted Answer

Youssef  Khmou
Youssef Khmou on 4 Feb 2013
Hi Gabriel :
I tested your code , it gives :
>> T
T =
3 5 7 9 11 13 15 17 19 21 23 25 27 29 31
>> winTime
winTime =
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
I guess that the winLen_Sec_desired equals 5, the real one winLenSec_Real equals 6 so that shift is seen between T and winTime
Temporal solution :
Increment with 1 this line in your code :
winTime = 1+time(winCenters);
they, now, give same result .
  3 Comments
Gabriele
Gabriele on 4 Feb 2013
I've tested also with other data and the shift is not
winLenSec_Real-winLenSec_desired;
for all the values in T
Youssef  Khmou
Youssef Khmou on 4 Feb 2013
Edited: Youssef Khmou on 4 Feb 2013
hi,
Post the counter example to see ,
with incremented winTime, it gave same vectors with these elementary signals :
%signal = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17];
%signal=sin(2*pi*(0:2:100)*0.25); % Fc=0.25 Hz
%signal=rand(10,1)';
%signal=square(2*pi*(0:2:100)*0.13); Fc=0.13 Hz
%signal=exp(-(0:2:10)).*cos(2*pi*0.10*(0:2:10)); % Fc=0.1 Hz
%signal=exp(j*2*pi*0.33*(0:0.1:10)); % Fc=0.33 Hz
signal=randint(10,1);

Sign in to comment.

More Answers (0)

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!