I'm trying to test the specgram function located in the signal package in octave but I'm a little confused at the variables in specgram. What I would like to do is be able to get the specgram data into an array that will show the frequency and the length of time when the frequency starts and stops.

See example code below: I was trying to get the array to show that for the length of t1 will be $7 \textrm{ Hz}$, t2 will be $12\textrm{ Hz}$ and for t3 will be $2\textrm{ Hz}$. How can I go about doing this?

I'm using ubuntu 12.04 and octave 3.2.4 and the signal pacakage 1.0.11

% combines sig with spectra plot
clear all,clc,tic;
t1=linspace(0,2*pi,fs/0.1); %need to round off no decimals
t2=linspace(0,2*pi,fs/0.3); %need to round off no decimals
t3=linspace(0,2*pi,fs/0.6); %need to round off no decimals

%Create signal in different arrays

%append arrays to test specgram
yt = [y1 y2 y3];

%plot(yt) %will show combined plot

%Spectrum section
yts=specgram(yt',20,500, 2,1);
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
  • 2
    $\begingroup$ It looks like you are effectively changing the sample rate when you go from t1 to t2 to t3. Why are you doing that? $\endgroup$
    – Jim Clay
    Aug 30 '12 at 13:29
  • $\begingroup$ I tried the code above in octave, and it does not work. Also, when using 'specgram(x)' instead i does display a spectrogram, but the frequencies are not right. does anybody know how to get them right? $\endgroup$
    – Michael
    May 11 '15 at 2:25
  • $\begingroup$ Write new question! Write what is "code above", write what is "are not right" and what is "get them right". $\endgroup$
    – SergV
    May 11 '15 at 2:41
  • $\begingroup$ If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. $\endgroup$
    – Matt L.
    May 11 '15 at 7:43

Try this:

fs = 100;
t = 0:1/fs:10;
x = [sin(2*pi*7*t), sin(2*pi*12*t), sin(2*pi*2*t)];
specgram(x,256,1000, [],200);

I have reduced the sampling frequency because the three frequencies in your signal are quite low. In the specgram() function, I have increased the FFT size to 256 and used the default hanning(256) window with 200/256 $\approx$ 80% overlap. You can see three horizontal lines at the three frequencies of interest.


Not the answer you're looking for? Browse other questions tagged or ask your own question.