Examples in Matlab and Octave
Matlab for Computing Spectrograms
Matlab listing: testspectrogram.mSearch Spectral Audio Signal Processing
Would you like to be notified by email when Julius Orion Smith III publishes a new entry into his blog?
% testspectrogram.m fs=22050; % sampling frequency D=1; % test signal duration in seconds L = ceil(fs*D)+1; % signal duration in samples n = 0:L-1; % discrete-time axis (samples) t = n/fs; % discrete-time axis (sec) %c = ones(1,L); % dc test (good first OLA check) c = chirp(t,0,D,fs/2); % sine sweep from 0 Hz to fs/2 Hz windur = 0.01; % window length in sec Modd = 1; % Set window parity (class can just use odd) if Modd % Test odd window length: M = 2*round((windur*fs-1)/2)+1; % hop = (M-1)/2; hop = M; nfft = 2^(3+nextpow2(M)); % w = hamming(M); w(1)=0; % Necessary for COLA property w = boxcar(M); sw = ola(w,hop,nfft); % find COLA gain olaGain = max(sw); % expect 1.08 for Hamming window w = w / olaGain; % normalize system for unity gain x = [zeros(1,M),c(:)',zeros(1,M)]; % add room for 0-phase processing figure(1); X=spectrogram(x,nfft,fs,w,-hop,1); % or spectrogram(x,nfft,fs,w,M-hop); else % Test even window length (COLA not exact) M = 2*round((windur*fs)/2); hop = M/2; nfft = 2^(3+nextpow2(M)); w = hamming(M); sw = ola(w,hop,nfft); % find COLA gain olaGain = max(sw); % expect 1.08 for Hamming window w = w / olaGain; % normalize system for unity gain x = [zeros(1,M),c(:)',zeros(1,M)]; % zero-phase processing figure(1); X = spectrogram(x,nfft,fs,w,-hop,1); % or spectrogram(x,nfft,fs,w,M-hop); end title('Spectrogram of original test signal'); print -depsc 'testspectrogram.eps'; % write plot to disk in PS format
