tomwalters@0: % tool tomwalters@0: % tomwalters@0: % INPUT VALUES: tomwalters@0: % tomwalters@0: % RETURN VALUE: tomwalters@0: % tomwalters@0: % bleeck@3: % This external file is included as part of the 'aim-mat' distribution package bleeck@3: % (c) 2011, University of Southampton bleeck@3: % Maintained by Stefan Bleeck (bleeck@gmail.com) bleeck@3: % download of current version is on the soundsoftware site: bleeck@3: % http://code.soundsoftware.ac.uk/projects/aimmat bleeck@3: % documentation and everything is on http://www.acousticscale.org tomwalters@0: tomwalters@0: function saimakeaimmovie(varargin) tomwalters@0: tomwalters@0: temp_sound_file_name='temp_sound.wav'; % must be the same as in getaiffs!!! tomwalters@0: tomwalters@0: if nargin<2 % only one parameter -> read file tomwalters@0: if size(varargin)==1 tomwalters@0: makefilename=varargin{1}; tomwalters@0: else tomwalters@0: makefilename='lastrun.genmovie'; tomwalters@0: end tomwalters@0: % fprintf('movie is produced from file %s from aifffile "makemovie_temp.aif"\n!',makefilename); tomwalters@0: % fprintf('movie is produced from file ''%s''\n',makefilename); tomwalters@0: else tomwalters@0: makefilename='lastrun.genmovie'; tomwalters@0: generateparameterfile(makefilename,varargin); tomwalters@0: end tomwalters@0: arguments=readparameterfile(makefilename); tomwalters@0: tomwalters@0: tomwalters@0: str_moviefile=getargument(arguments,'moviefile'); tomwalters@0: str_aifffile=getargument(arguments,'aifffile'); tomwalters@0: str_movie_duration=getargument(arguments,'movie_duration'); tomwalters@0: str_movie_start_time=getargument(arguments,'movie_start_time'); tomwalters@0: str_AuditoryImageFormat=getargument(arguments,'AuditoryImageFormat'); tomwalters@0: str_TimeIntervalUnits=getargument(arguments,'TimeIntervalUnits'); tomwalters@0: str_colormap=getargument(arguments,'colormap'); tomwalters@0: str_minimum_time_interval=getargument(arguments,'minimum_time_interval'); tomwalters@0: str_maximum_time_interval=getargument(arguments,'maximum_time_interval'); tomwalters@0: str_sound_sample_rate=getargument(arguments,'sound_sample_rate'); tomwalters@0: str_sound_endian=getargument(arguments,'sound_endian'); tomwalters@0: str_linewidth=getargument(arguments,'linewidth'); tomwalters@0: str_echo=getargument(arguments,'echo'); tomwalters@0: str_showtime=getargument(arguments,'showtime'); tomwalters@0: str_showtextname=getargument(arguments,'showtextname'); tomwalters@0: str_showtexttime=getargument(arguments,'showtexttime'); tomwalters@0: str_plotscale=getargument(arguments,'plotscale'); tomwalters@0: str_profile_scale=getargument(arguments,'profile_scale'); tomwalters@0: str_producemovie=getargument(arguments,'producemovie'); tomwalters@0: tomwalters@0: % data originally used in readaiff, but needed here also: tomwalters@0: str_framespersecond=getargument(arguments,'framespersecond'); tomwalters@0: str_sai_picturespersecond=getargument(arguments,'sai_picturespersecond'); tomwalters@0: str_nap_picturespersecond=getargument(arguments,'nap_picturespersecond'); tomwalters@0: str_model=getargument(arguments,'modelfile'); tomwalters@0: str_soundcommand=getargument(arguments,'soundfile'); tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % if a movie is produced at all, or just the sais tomwalters@0: if isempty(str_producemovie) % tomwalters@0: producemovie='sai_and_nap'; tomwalters@0: else tomwalters@0: producemovie=str_producemovie; tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % scale the amplitude of the whole picture up tomwalters@0: if isempty(str_plotscale) % default frames per second tomwalters@0: plot_scale=1; tomwalters@0: else tomwalters@0: eval(sprintf('plot_scale=%s;',str_plotscale)); tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % scale the amplitude of the profiles (only when profiles are plotted of course) tomwalters@0: if isempty(str_profile_scale) % default frames per second tomwalters@0: profile_scale=1; tomwalters@0: else tomwalters@0: eval(sprintf('profile_scale=%s;',str_profile_scale)); tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % show time and number of frame tomwalters@0: if isempty(str_showtime) % default frames per second tomwalters@0: showtime=1; tomwalters@0: else tomwalters@0: if strcmp(str_showtime,'off') tomwalters@0: showtime=0; tomwalters@0: else tomwalters@0: showtime=1; tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % frames per second tomwalters@0: if isempty(str_framespersecond) % default frames per second tomwalters@0: framespersecond=12; tomwalters@0: else tomwalters@0: eval(sprintf('framespersecond=%s;',str_framespersecond)); tomwalters@0: end tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % pictures per second tomwalters@0: if isempty(str_sai_picturespersecond) % default every ms tomwalters@0: sai_picturespersecond=1000; tomwalters@0: else tomwalters@0: eval(sprintf('sai_picturespersecond=%s;',str_sai_picturespersecond)); tomwalters@0: end tomwalters@0: if isempty(str_nap_picturespersecond) % default every ms tomwalters@0: nap_picturespersecond=1000; tomwalters@0: else tomwalters@0: eval(sprintf('nap_picturespersecond=%s;',str_nap_picturespersecond)); tomwalters@0: end tomwalters@0: tomwalters@0: if isempty(str_moviefile) % default name of the movie tomwalters@0: [dumy_path,tempmodelname,ext,versn] = fileparts(str_model); tomwalters@0: [dumy_path,tempsoundname,ext,versn] = fileparts(str_soundcommand); tomwalters@0: moviefile=sprintf('soundfile_%s_model_%s.mov',tempsoundname,tempmodelname); tomwalters@0: else tomwalters@0: [dumy_path,tempmodelname,ext,versn] = fileparts(str_moviefile); tomwalters@0: if strcmp(ext,'') tomwalters@0: moviefile=sprintf('%s.mov',str_moviefile); tomwalters@0: else tomwalters@0: moviefile=str_moviefile; tomwalters@0: end tomwalters@0: end tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % sound-file. can be a command or a file tomwalters@0: if isempty(str_soundcommand) % tomwalters@0: error('soundcommand must be given'); tomwalters@0: else tomwalters@0: soundcommand=str_soundcommand; tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % construct the aiff-file with a call to the dsam-routine: tomwalters@0: tomwalters@0: % if aifffile is given from a previous run, than simply load it: tomwalters@0: if ~isempty(str_aifffile) tomwalters@0: allframes=SBReadAiff(str_aifffile); % returns all info in a struct tomwalters@0: else tomwalters@0: allframes=getaiffs(makefilename); tomwalters@0: end tomwalters@0: complete_nap=allframes(1); tomwalters@0: nr_channels=getnrchannels(complete_nap); tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: tomwalters@0: tomwalters@0: % grafix=1; tomwalters@0: select_channels=1:nr_channels; tomwalters@0: select_single_channels=3; tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % read the sound command and transfere the data to the buffer tomwalters@0: if ~isempty(str_movie_duration) % default frames per second tomwalters@0: eval(sprintf('movie_duration=%s;',str_movie_duration)); tomwalters@0: eval(sprintf('movie_start_time=%s;',str_movie_start_time)); tomwalters@0: [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian,movie_start_time,movie_duration); tomwalters@0: else tomwalters@0: movie_start_time=0; tomwalters@0: [sounddata,samplerate,bits,endian]=producesounddata(soundcommand,temp_sound_file_name,str_sound_sample_rate,str_sound_endian); tomwalters@0: movie_duration=length(sounddata)/samplerate; tomwalters@0: end tomwalters@0: sampletime=1/samplerate; tomwalters@0: % the duration of the whole video tomwalters@0: videolength=size(sounddata,1)/samplerate; tomwalters@0: % so long is one picture: tomwalters@0: tomwalters@0: if ~strcmp(producemovie,'sai_only') tomwalters@0: sai_pictureduration=1/sai_picturespersecond; tomwalters@0: else tomwalters@0: sai_pictureduration=1/framespersecond; tomwalters@0: end tomwalters@0: % so many frames is the video size in the end: tomwalters@0: nr_sai_pictures=floor(videolength/sai_pictureduration); tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % start processing the data tomwalters@0: % first: 2DAT on the nap: tomwalters@0: clear napoptions; tomwalters@0: napoptions.time_constant_factor=6; %makes the time constant for the time adaption longer tomwalters@0: % napoptions.frequency_constant_factor=0.8; % the influence of the neighboring channels tomwalters@0: napoptions.frequency_constant_factor=0.0; % the influence of the neighboring channels tomwalters@0: tomwalters@0: disp('calculating 2D adaptive threshold...'); tomwalters@0: sharpnap=twoDat(complete_nap,napoptions); % two dimensional adaptive thresholding tomwalters@0: tomwalters@0: % strobeoptions tomwalters@0: strobeoptions.strobe_decay_time=0.02; % in this time the strobe threshold decreases to 0 tomwalters@0: strobeoptions.parabel_heigth=1.2; % times the nap height at that point tomwalters@0: % strobeoptions.parabel_width=0.01; % ms tomwalters@0: strobeoptions.parabel_width_in_cycles=1.5; % cycles tomwalters@0: strobeoptions.influence_nap_heigth=0; % influcence of the height of the nap at the point, where the strobe was elicited tomwalters@0: strobeoptions.nr_cyclyes_to_wait=1.5; % wait a little longer then once cycle for the real strobe tomwalters@0: strobeoptions.select_channels=select_channels; tomwalters@0: tomwalters@0: disp('calculating strobes...'); tomwalters@0: [allstrobeprocesses,allthresholds]=findstrobes(sharpnap,strobeoptions); tomwalters@0: tomwalters@0: tomwalters@0: % figure(1);clf tomwalters@0: % plotwithstrobes(sharpnap,allstrobeprocesses); tomwalters@0: % tomwalters@0: % return tomwalters@0: if strcmp(producemovie,'sai_and_nap') tomwalters@0: step=1/nap_picturespersecond; tomwalters@0: graphic_times=movie_start_time:step:movie_start_time+movie_duration; tomwalters@0: maxheight=5; tomwalters@0: maxnap=max(sharpnap); tomwalters@0: next_nap_picture=step; tomwalters@0: current_graphic=1; tomwalters@0: f1=figure(1);clf; tomwalters@0: set(f1,'position',[ 640 1 640 400]) tomwalters@0: else tomwalters@0: next_nap_picture=inf; % never this graphic tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: % set a variable for the current starting time of each frame for plotting tomwalters@0: for i=1:nr_sai_pictures tomwalters@0: start_times(i)=(i-1)*sai_pictureduration+movie_start_time; tomwalters@0: sai_graphic_times(i)=start_times(i)+sai_pictureduration-sampletime; tomwalters@0: end tomwalters@0: tomwalters@0: next_sai_graphic=sai_graphic_times(1); tomwalters@0: current_sai_graphic=1; tomwalters@0: tomwalters@0: fprintf('producing stobed image:\n'); tomwalters@0: % We start the time at zero: tomwalters@0: current_time=movie_start_time; tomwalters@0: nr_dots=getnrpoints(sharpnap); tomwalters@0: clear saioptions; tomwalters@0: saioptions.start_time=start_times(i); tomwalters@0: saioptions.maxdelay=0.035; tomwalters@0: saioptions.strobe_weight_alpha=1; % alpha parameter in the weightening of the single strobes tomwalters@0: saioptions.phase_adjustment=1.5; % after so many cycles the change of weight takes effect tomwalters@0: saioptions.mindelay_in_cycles=1.5; % so many cycles are not filled in the sai-buffer tomwalters@0: saioptions.buffer_memory_decay=0.04; % time for the buffer to go from 100% to 0 tomwalters@0: saioptions.weightthreshold=0.001; % only thresholds bigger than 1% tomwalters@0: if ~strcmp(producemovie,'no') tomwalters@0: saioptions.grafix=1; % no graphical outout during processing tomwalters@0: end tomwalters@0: saioptions.select_channels=select_channels; tomwalters@0: saioptions.const_memory_decay=power(0.5,1/(saioptions.buffer_memory_decay*samplerate)); % the amount per sampletime tomwalters@0: saioptions.signal_start_time=movie_start_time; tomwalters@0: tomwalters@0: % construct the starting SAI with zeros tomwalters@0: nrdots_insai=round(saioptions.maxdelay*samplerate); tomwalters@0: svals=zeros(getnrchannels(sharpnap),nrdots_insai); tomwalters@0: fstruct.outputTimeOffset=0; tomwalters@0: fstruct.totalframetime=saioptions.maxdelay; tomwalters@0: current_sai=frame(svals,fstruct,getcf(sharpnap)); tomwalters@0: current_sai_struct.data=current_sai; tomwalters@0: for i=select_channels tomwalters@0: if length(allstrobeprocesses{i}.strobes) > 0 tomwalters@0: info.next_strobe=allstrobeprocesses{i}.strobes(1); % the next strobe in line tomwalters@0: else tomwalters@0: info.next_strobe=inf; % no strobe tomwalters@0: end tomwalters@0: info.current_strobe_nr=0; % the current number of the strobe which is processed tomwalters@0: info.strobeprocesses=[]; % no active strobes in queue tomwalters@0: info.strobe_adjust_phase=inf; % the next update of weights tomwalters@0: info.was_adjusted=0; tomwalters@0: current_sai_struct.info{i}=info; tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: if ~strcmp(producemovie,'no') tomwalters@0: MakeQTMovie('start',moviefile); tomwalters@0: MakeQTMovie('size', [640 400]); tomwalters@0: MakeQTMovie('quality', 0.9); % reduces the size tomwalters@0: % only, to know, how many channels we have: tomwalters@0: for i=1:nr_sai_pictures tomwalters@0: fprintf('*'); tomwalters@0: end tomwalters@0: fprintf('\n'); tomwalters@0: end tomwalters@0: tomwalters@0: for i=1:nr_dots tomwalters@0: % fprintf('*'); tomwalters@0: current_time=current_time+sampletime; tomwalters@0: current_sai_struct=updatesai2(sharpnap,current_sai_struct,allstrobeprocesses,saioptions,current_time); tomwalters@0: tomwalters@0: if ~strcmp(producemovie,'no') tomwalters@0: if current_time>=next_nap_picture tomwalters@0: if current_graphic maxtime tomwalters@0: disp_start_time = maxtime-disp_duration; tomwalters@0: end tomwalters@0: tomwalters@0: plotoptions.sharpnap=sharpnap; tomwalters@0: plotoptions.allthresholds=allthresholds; tomwalters@0: plotoptions.current_sai_struct=current_sai_struct; tomwalters@0: plotoptions.allstrobeprocesses=allstrobeprocesses; tomwalters@0: plotoptions.maxnap=maxnap; tomwalters@0: plotoptions.maxheight=maxheight; tomwalters@0: plotoptions.current_time=current_time; tomwalters@0: plotoptions.saioptions=saioptions; tomwalters@0: plotoptions.select_channels=select_single_channels; tomwalters@0: plotoptions.disp_start_time=disp_start_time; tomwalters@0: plotoptions.disp_duration=disp_duration; tomwalters@0: tomwalters@0: plotoptions=doplot(plotoptions); tomwalters@0: end tomwalters@0: if current_time > next_sai_graphic tomwalters@0: fprintf('*'); tomwalters@0: current_sai_struct.data=setcurrentframestarttime(current_sai_struct.data,current_time); tomwalters@0: all_finished_frames(current_sai_graphic)=current_sai_struct.data; tomwalters@0: if current_sai_graphicmaxheight tomwalters@0: maxheight=maxheight*1.1; tomwalters@0: end tomwalters@0: axis([1 getnrpoints(saibuffer),0,maxheight]); tomwalters@0: % text(300,maxheight/1.5,sprintf('sum: %3.2f',sum(saibuffer))); tomwalters@0: text(300,maxheight/2,sprintf('#time: %dms',floor(current_time*1000))); tomwalters@0: text(300,maxheight/2.8,sprintf('#Strobes: %d',nr_active_strobes)); tomwalters@0: tomwalters@0: mysubplot(1,1,1,[0.65 0.5 0.35 0.3]); tomwalters@0: if options.oldhandle2~=0 tomwalters@0: delete(options.oldhandle2); tomwalters@0: end tomwalters@0: options.oldhandle2=gca; tomwalters@0: set(gca,'Xtick',[]); tomwalters@0: set(gca,'Ytick',[]); tomwalters@0: plot(threshold,'g'); tomwalters@0: hold on tomwalters@0: plot(single_channel,'-'); tomwalters@0: ylabel(''); tomwalters@0: if max(single_channel)>maxnap tomwalters@0: maxnap=maxnap*1.1; tomwalters@0: end tomwalters@0: if max(threshold)>maxnap tomwalters@0: maxnap=maxnap*1.1; tomwalters@0: end tomwalters@0: axis([1 getnrpoints(single_channel),0,maxnap]); tomwalters@0: for k=1:length(strobe_process) tomwalters@0: gc=plot(time2bin(single_channel,strobe_process(k).time),strobe_process(k).nap_value,'.r'); tomwalters@0: suze=log(200*strobe_process(k).weight)*5; tomwalters@0: suze=max(suze,5); tomwalters@0: set(gc,'MarkerSize',suze); tomwalters@0: end tomwalters@0: drawnow; tomwalters@0: else % end only one channel tomwalters@0: % from here: more then one channel tomwalters@0: % figure(1);clf; tomwalters@0: % partthres=getpart(allthresholds,disp_start_time,current_time); tomwalters@0: % str.plotcolor='g'; tomwalters@0: % plot(partthres,str); hold on tomwalters@0: % partnap=getpart(sharpnap,disp_start_time,disp_start_time+disp_duration); tomwalters@0: % plot(partnap); hold on tomwalters@0: % tomwalters@0: % % for k=1:length(strobe_process) tomwalters@0: % % gc=plot(time2bin(single_channel,strobe_process(k).time),strobe_process(k).nap_value,'.r'); tomwalters@0: % % suze=log(200*strobe_process(k).weight)*5; tomwalters@0: % % suze=max(suze,5); tomwalters@0: % % set(gc,'MarkerSize',suze); tomwalters@0: % % end tomwalters@0: % tomwalters@0: % tomwalters@0: % tomwalters@0: % sai=current_sai_struct.data; tomwalters@0: % % figure(2);clf; tomwalters@0: % plot(sai); tomwalters@0: % tomwalters@0: % drawnow; tomwalters@0: end tomwalters@0: return tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: function options=plotsai(current_sai_struct,current_time,options) tomwalters@0: mysubplot(1,1,1,[-0.05 0 0.7 0.9]); tomwalters@0: gopt.current_frame = current_sai_struct.data; tomwalters@0: gopt.maximum_time_interval = 35; tomwalters@0: gopt.minimum_time_interval= 0; tomwalters@0: gopt.is_log=0; tomwalters@0: gopt.plot_scale=1/5; tomwalters@0: gopt.show_time=1; tomwalters@0: gopt.time_reversed=1; tomwalters@0: gopt.frequency_profile_scale=400; tomwalters@0: gopt.time_profile_scale=0.03; tomwalters@0: gopt.options=options; tomwalters@0: options=AIFrePtiP(gopt); tomwalters@0: drawnow; tomwalters@0: return tomwalters@0: tomwalters@0: function plotsaionly(current_sai_struct,current_time) tomwalters@0: gopt.current_frame = current_sai_struct.data; tomwalters@0: gopt.maximum_time_interval = 35; tomwalters@0: gopt.minimum_time_interval= 0; tomwalters@0: gopt.is_log=0; tomwalters@0: gopt.plot_scale=1/5; tomwalters@0: gopt.show_time=1; tomwalters@0: gopt.time_reversed=1; tomwalters@0: gopt.frequency_profile_scale=400; tomwalters@0: gopt.time_profile_scale=0.03; tomwalters@0: clf tomwalters@0: AIFrePtiP(gopt); tomwalters@0: drawnow; tomwalters@0: return tomwalters@0: tomwalters@0: function plotsaiprofiles(current_sai_struct,current_time) tomwalters@0: gopt.current_frame = current_sai_struct.data; tomwalters@0: gopt.frequency_profile_scale=0.002; tomwalters@0: gopt.time_profile_scale=0.01;clf tomwalters@0: combFrePtiP(gopt); tomwalters@0: drawnow; tomwalters@0: return