annotate toolboxes/MIRtoolbox1.3.2/AuditoryToolbox/spectrogram.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [array,raw] = spectrogram(wave,segsize,nlap,ntrans);
Daniel@0 2 %function array = spectrogram(wave,segsize,nlap,ntrans);
Daniel@0 3 % defaults spectrogram(wave,128,8,4)
Daniel@0 4 % nlap is number of hamming windows overlapping a point;
Daniel@0 5 % ntrans is factor by which transform is bigger than segment;
Daniel@0 6 % returns a spectrogram 'array' with fourth root of power,
Daniel@0 7 % filter smoothed and formatted for display.
Daniel@0 8
Daniel@0 9 % Added option to return raw spectrogram.... Malcolm 5/26/95
Daniel@0 10 % Added code so that input could be any direction ... Malcolm 5/26/95
Daniel@0 11 % (c) 1998 Interval Research Corporation
Daniel@0 12
Daniel@0 13 if nargin < 4; ntrans=4; end
Daniel@0 14 if nargin < 3; nlap=8; end
Daniel@0 15 if nargin < 2; segsize=128; end
Daniel@0 16
Daniel@0 17 [r c] = size(wave);
Daniel@0 18 if (r < c)
Daniel@0 19 wave = filter([1 -0.95],[1],wave');
Daniel@0 20 else
Daniel@0 21 wave = filter([1 -0.95],[1],wave);
Daniel@0 22 end
Daniel@0 23
Daniel@0 24 s = length(wave);
Daniel@0 25 nsegs = floor(s/(segsize/nlap))-nlap+1;
Daniel@0 26 array = zeros(ntrans/2*segsize,nsegs);
Daniel@0 27 window = 0.54-0.46*cos(2*pi/(segsize+1)*(1:segsize)');
Daniel@0 28 for i = 1:nsegs
Daniel@0 29 seg = zeros(ntrans*segsize,1); % leave half full of zeroes
Daniel@0 30 seg(1:segsize) = ...
Daniel@0 31 window.*wave(((i-1)*segsize/nlap+1):((i+nlap-1)*segsize/nlap));
Daniel@0 32 seg = abs(fft(seg));
Daniel@0 33 % reverse for image display
Daniel@0 34 array(:,i) = seg(((ntrans/2*segsize)+1):(ntrans*segsize));
Daniel@0 35 end
Daniel@0 36
Daniel@0 37 if nargout > 1
Daniel@0 38 raw = array;
Daniel@0 39 end
Daniel@0 40
Daniel@0 41 array = array .* array; % back into power domain for smoothing
Daniel@0 42
Daniel@0 43 for i=1:nsegs % smooth the spectral slices
Daniel@0 44 array(:,i) = filter([.2 1 .2],[1],array(:,i));
Daniel@0 45 end
Daniel@0 46
Daniel@0 47 for i=1:ntrans/2*segsize % smooth the channels
Daniel@0 48 array(i,:) = filter([.2 1 .2],[1],array(i,:));
Daniel@0 49 end
Daniel@0 50
Daniel@0 51 % compress with square root of amplitude (fourth root of power)
Daniel@0 52 off = 0.0001*max(max(array)); % low end stabilization offset,
Daniel@0 53 array = (off+array).^0.25-off^0.25; % better than a threshold hack!
Daniel@0 54 array = 255/max(max(array))*array;
Daniel@0 55
Daniel@0 56
Daniel@0 57