No BSD License  

Highlights from
Sliding Spectrometer

image thumbnail
from Sliding Spectrometer by Zachary Moratto
It's a sliding spectrometer

SlidingSpectrometer(varargin)
function varargout = SlidingSpectrometer(varargin)
% SLIDINGSPECTROMETER M-file for SlidingSpectrometer.fig
%      SLIDINGSPECTROMETER, by itself, creates a new SLIDINGSPECTROMETER or raises the existing
%      singleton*.
%
%      H = SLIDINGSPECTROMETER returns the handle to a new SLIDINGSPECTROMETER or the handle to
%      the existing singleton*.
%
%      SLIDINGSPECTROMETER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SLIDINGSPECTROMETER.M with the given input arguments.
%
%      SLIDINGSPECTROMETER('Property','Value',...) creates a new SLIDINGSPECTROMETER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before SlidingSpectrometer_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to SlidingSpectrometer_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help SlidingSpectrometer

% Last Modified by GUIDE v2.5 12-Jun-2007 15:15:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SlidingSpectrometer_OpeningFcn, ...
                   'gui_OutputFcn',  @SlidingSpectrometer_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

%--------------------------------------------------------------------------
% --- Executes just before SlidingSpectrometer is made visible.
function SlidingSpectrometer_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SlidingSpectrometer (see VARARGIN)

% Choose default command line output for SlidingSpectrometer
handles.output = hObject;

%Adding my own code for startup here.
handles.refreshFreq = 100;	% Hz
handles.refreshRate = 1/handles.refreshFreq;	%secs
handles.sampleRate = 8000;	%Hz
handles.NFFT =256;				%The length of the FFT
handles.window = hamming(256);		%The window mask for the FFT
handles.NoChannels = 24;	%The number of channels used to convert to mel spectrum
handles.melMatrix = flipud(fliplr(melfiltermatrix(handles.sampleRate,handles.NFFT,handles.NoChannels)));
handles.melMatrix = handles.melMatrix;

%Setting up the analog
ai = analoginput('winsound');
handles.ai = ai;
addchannel(handles.ai,1);	%Just adding one channel, microphone is mono
handles.sampleRate = setverify(ai,'SampleRate',handles.sampleRate);
set(handles.FreqMarker,'String',sprintf('Sample Req: %6.1f Hz',handles.sampleRate));
handles.ai.TimerPeriod = handles.refreshRate;
spt = round(handles.sampleRate*handles.refreshRate);
handles.numSample = spt;
handles.ai.SamplesPerTrigger = spt;

handles.sampleBacklog = zeros(256,1);

%My spectrometer is 500 by 128 pixels

%Setting up the displays
axes(handles.Spectrometer);
handles.Spectrometer = image(zeros(128,500,1),...
	'Clipping','off');
colormap(hot(256));
%Note: Energymeter is 1000x64
axes(handles.Energymeter);
%It's special, it gets full color (!!!)
handles.Energymeter = image(zeros(64,500,3),...
	'Clipping','off');
axes(handles.MelSpectrometer);
handles.MelSpectrometer = image(zeros(handles.NoChannels,500,1),...
	'Clipping','off');

guidata(hObject, handles);
set(handles.ai,'TriggerRepeat',Inf);
set(handles.ai,'TimerFcn',{@Ai_Callback,handles});
% Update handles structure
guidata(hObject, handles);

start(handles.ai);

% UIWAIT makes SlidingSpectrometer wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%--------------------------------------------------------------------------
% --- Outputs from this function are returned to the command line.
function varargout = SlidingSpectrometer_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

%--------------------------------------------------------------------------
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

stop(handles.ai);
delete(handles.ai);

% Hint: delete(hObject) closes the figure
delete(hObject);

%--------------------------------------------------------------------------
% --- Executes when the timer resets.
function Ai_Callback(hObject,eventdata,handles)

% %Grabbing the data from the buffer
 try
 	y = getdata(handles.ai,handles.numSample);
 	flushdata(handles.ai);
 catch
 	y = [];
 end

 %Getting rid of the bad samples.
 y = y(~isnan(y));
 y = Preemphasis (y);

 %Update the backlog for processing by FFT
 handles.sampleBacklog(1:176) = handles.sampleBacklog(81:end);
 handles.sampleBacklog(177:end) = y;
 
 if ~isempty(y)
	z = get(handles.Spectrometer,'cdata');
	Y = fft(handles.sampleBacklog.*handles.window,handles.NFFT)/handles.NFFT;
	z(:,1:end-1) = z(:,2:end);
	Y = Y(1:handles.NFFT/2);
	Y = flipud(8000*abs(Y));
	z(:,end) = Y;
	set(handles.Spectrometer,'cdata',z);
	
	z = get(handles.Energymeter,'cdata');
	z(:,1:end-1,:) = z(:,2:end,:);
	z(:,end,:) = ones(64,1,3); %Used to make white background
	e = round(sum(Y)/200);
	if e > 63
		e = 63;
	end
	if e > 0	%Is it in the threshold
		z(64-e,end,2:3) = 0;
	else		%Or is it just shit?
		z(64-e,end,1:2) = 0;
	end
	set(handles.Energymeter,'cdata',z);
	
	z = get(handles.MelSpectrometer,'cdata');
	z(:,1:end-1) = z(:,2:end);
	z(:,end) = Y'*handles.melMatrix';
	set(handles.MelSpectrometer,'cdata',z);
 end

Contact us at files@mathworks.com