matlabsignal-processingfftwavelethaar-wavelet

how to alter the range of values of a plot generated by spectrogram function


In the below posted image, I am trying to get TFR using STFT. In the code posted, I specified the paramerter T = 0:.001:1; and when I modify it to be, for an example, T = 0:.001:2; the values range on the horizontal axis of the plot changes, despite it is labelled Frequency.

Now, I want to change the ranges of values of the horizontal and the vertical axes on the shown plot. How can I do that?

NOTE: the code used to generate the shown plot is:

T = 0:.001:1;
spectrogram(x4,128,50,NFFT);

CODE:

% Time specifications:
 Fs = 8000;                       % samples per second
 dt = 1/Fs;                       % seconds per sample
 StopTime = 1;                    % seconds
  t = (0:dt:StopTime-dt);             % seconds

  t1 = (0:dt:.25);
   t2 = (.25:dt:.50);
  t3 = (.5:dt:.75);
  t4 = (.75:dt:1);

  %two freqs. = abs(f1 - f2), that's why, x1 is plotted with 2 freqs.
  x1 = (10)*sin(2*pi*30*t1);
  x2 = (10)*sin(2*pi*60*t2) + x1;
  x3 = (10)*sin(2*pi*90*t3) + x2;
  x4 = (10)*sin(2*pi*120*t4) + x3;
  %x5 = (10) * sin(2*pi*5*t5);
  %x6 = x1 + x2 + x3 + x4 + x5;

  NFFT = 2 ^ nextpow2(length(t));     % Next power of 2 from length of y
  Y    = fft(x3, NFFT);
  f    = Fs / 2 * linspace(0, 1, NFFT/2 + 1);
  figure;
  plot(f(1:200), 2 * abs( Y( 1:200) ) );

  T = 0:.001:1;
  spectrogram(x4,10,9,31);
   axis(get(gcf,'children'), [0, 1,0,100]);

% Plot the signal versus time:
figure;
xlabel('time (in seconds)');
ylabel('Amplitude');
title('non-stationary Signal versus Time');

 hold on
 plot(t1,x1,'r');
plot(t2,x2,'g');
plot(t3,x3,'b');
plot(t4,x4,'black');
%plot(t5,x5,'y');
%plot(t, x6,'black');
legend('x1 = (10)*sin(2*pi*15*t1) + (10)*sin(2*pi*8*t1)', 'x2 = (10)*sin(2*pi*25*t2) + x1',   
'x3 = (10)*sin(2*pi*50*t3) + x2', 'x4 = (10)*sin(2*pi*75*t4) + x3', ...
'Location',  'SouthWest');

image

enter image description here

new Result_1 enter image description here


Solution

  • Idea: get the axis the was used to plot the spectrogram and set its properties accordingly. For example, supposing you'd want to restrict the x range to [0, 0.5] and y to [100, 200], then:

    %'old code here'
    %' . . . '
    spectrogram(x4,128,50,NFFT);
    
    %'new code here'
    axis(get(gcf,'children'), [0, 0.5, 100, 200]);
    

    Explanation: The added one-liner gets the child handle from the current figure gcf (wich is assumed to be created by spectrogram), then sets it's range to [xmin, xmax, ymin, ymax] via axis call.

    Nota Bene: I assume that you just need to re-scale the axis, not re-compute the spectrogram for different data.

    Also I assume the spectrogram doesn't share its figure with other axes.

    Also, extending the axis range rather than restricting it might not give you the expected results (in a word: is ugly).