Code covered by the BSD License  

Highlights from
Echo Signal

image thumbnail

Echo Signal

by

 

13 Feb 2014 (Updated )

This exercise demonstrates how and echo can be introduced into a signal.

Callbacks_echo_signal_G25(f,C,start_path)
function Callbacks_echo_signal_G25(f,C,start_path)
%SENSE COMPUTER AND SET FILE DELIMITER
switch(computer)				
    case 'MACI64',		char= '/';
    case 'GLNX86',  char='/';
    case 'PCWIN',	char= '\';
    case 'PCWIN64', char='\';
    case 'GLNXA64', char='/';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=C{1,1};
y=C{1,2};
a=C{1,3};
b=C{1,4};
u=C{1,5};
v=C{1,6};
m=C{1,7};
n=C{1,8};
lengthbutton=C{1,9};
widthbutton=C{1,10};
enterType=C{1,11};
enterString=C{1,12};
enterLabel=C{1,13};
noPanels=C{1,14};
noGraphicPanels=C{1,15};
noButtons=C{1,16};
labelDist=C{1,17};%distance that the label is below the button
noTitles=C{1,18};
buttonTextSize=C{1,19};
labelTextSize=C{1,20};
textboxFont=C{1,21};
textboxString=C{1,22};
textboxWeight=C{1,23};
textboxAngle=C{1,24};
labelHeight=C{1,25};
fileName=C{1,26};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PANELS
for j=0:noPanels-1
uipanel('Parent',f,...
'Units','Normalized',...
'Position',[x(1+4*j) y(1+4*j) x(2+4*j)-x(1+4*j) y(3+4*j)-y(2+4*j)]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%GRAPHIC PANELS
for i=0:noGraphicPanels-1
switch (i+1)
case 1
graphicPanel1 = axes('parent',f,...
'Units','Normalized',...
'Position',[a(1+4*i) b(1+4*i) a(2+4*i)-a(1+4*i) b(3+4*i)-b(2+4*i)],...
'GridLineStyle','--');
case 2
graphicPanel2 = axes('parent',f,...
'Units','Normalized',...
'Position',[a(1+4*i) b(1+4*i) a(2+4*i)-a(1+4*i) b(3+4*i)-b(2+4*i)],...
'GridLineStyle','--');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%TITLE BOXES
for k=0:noTitles-1
switch (k+1)
case 1
titleBox1 = uicontrol('parent',f,...
'Units','Normalized',...
'Position',[u(1+4*k) v(1+4*k) u(2+4*k)-u(1+4*k) v(3+4*k)-v(2+4*k)],...
'Style','text',...
'FontSize',textboxFont{k+1},...
'String',textboxString(k+1),...
'FontWeight',textboxWeight{k+1},...
'FontAngle',textboxAngle{k+1});
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%BUTTONS
for i=0:(noButtons-1)
enterColor='w';
if strcmp(enterType{i+1},'pushbutton')==1 ||strcmp(enterType{i+1},'text')==1
enterColor='default';
end
if (strcmp(enterLabel{1,(i+1)},'')==0 &&...
        strcmp(enterLabel{1,(i+1)},'...')==0) %i.e. there is a label
%creating a label for some buttons
uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i)-labelDist-labelHeight(i+1) ...
(m(2+2*i)-m(1+2*i)) labelHeight(i+1)],...
'Style','text',...
'String',enterLabel{i+1},...
'FontSize', labelTextSize(i+1),...
'HorizontalAlignment','center');
end
switch (i+1)
case 1
button1=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button1Callback);
case 2
button2=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button2Callback);
case 3
button3=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button3Callback);
case 4
button4=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button4Callback);
case 5
button5=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button5Callback);
case 6
button6=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button6Callback);
case 7
button7=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button7Callback);
case 8
button8=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button8Callback);
case 9
button9=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button9Callback);
case 10
button10=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',[m(1+2*i) n(1+2*i) (m(2+2*i)-m(1+2*i)) (n(2+2*i)-n(1+2*i))],...
'Style',enterType{i+1},...
'String',enterString{i+1},...
'FontSize', buttonTextSize(1+i),...
'BackgroundColor',enterColor,...
'HorizontalAlignment','center',...
'Callback',@button10Callback);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%USER CODE FOR THE VARIABLES AND CALLBACKS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
curr_file='abcd';
fileName='file_echo.wav';
fs=10000;
N=100;
Nm=100;
alpha=0.9;
directory_name='abcd';
wav_file_names='abcd';
writefilename='abcd';
file_info_string=' ';
stitle1=' ';
yout=[];
fname='';
nsamp=1;
xpp='abcd';

% spectrogram parameters
    winlen_WB=3;
    nfft_WB=1024;
    overlap=96;
    map_index=2;
    select_win=1;
    logLinear_index=1;
    dyn_range=60;
    
% converting window lengths from ms to samples
    winlen_WBsamples = fix(winlen_WB*0.001*fs); 
% overlap in samples based on window size
	overlap_WB=fix(overlap*winlen_WBsamples/100);
% selection of window
	w_WB = window(@hamming,winlen_WBsamples);

% Name the GUI
    set(f,'Name','echo_signal');

% CALLBACKS

% Callback for button1 -- Get Speech Files Directory
 function button1Callback(h,eventdata)
     directory_name=uigetdir(start_path,'dialog_title');
     A=strvcat(strcat((directory_name),[char,'*.wav']));
     struct_filenames=dir(A);
     wav_file_names={struct_filenames.name};
     set(button2,'String',wav_file_names);
     
 % once the popupmenu/drop down menu is created, by default, the first
 % selection from the popupmenu/drop down menu id not called
    indexOfDrpDwnMenu=1;
    
 % by default first option from the popupmenu/dropdown menu will be loaded
    [curr_file,fs]=loadSelection(directory_name,wav_file_names,indexOfDrpDwnMenu);
 end

% Callback for button2 -- Choose speech file for play and plot
 function button2Callback(h,eventdata)
     indexOfDrpDwnMenu=get(button2,'val');
     [curr_file,fs]=loadSelection(directory_name,wav_file_names,indexOfDrpDwnMenu);
 end

%*************************************************************************
% function -- load selection from designated directory and file
%
function [curr_file,fs]=loadSelection(directory_name,...
    wav_file_names,indexOfDrpDwnMenu);
%
% read in speech/audio file
% fin_path is the complete path of the .wav file that is selected
    fin_path=strcat(directory_name,char,strvcat(wav_file_names(indexOfDrpDwnMenu)));
 
% clear speech/audio file
    clear curr_file;
   
% read in speech/audio signal into curr_file; sampling rate is fs
    [curr_file,fs]=wavread(fin_path);
    
% create title information with file, sampling rate, number of samples
    fname=wav_file_names(indexOfDrpDwnMenu);
    FS=num2str(fs);
    nsamp=num2str(length(curr_file));
    file_info_string=strcat('  file: ',fname,', fs: ',FS,' Hz, nsamp:',nsamp);
end
%*************************************************************************

% Callback for button3 -- play and plot current speech file
 function button3Callback(h,eventdata)
% play out original speech file
     soundsc(curr_file,fs);
     
% gray scale map
	t=colormap(gray);
	colormap(1-t);

% create and plot wideband spectrogram on graphics Panel 2
        [B,BA,F,T]=create_spectrogram(curr_file,nfft_WB,fs,w_WB,dyn_range,logLinear_index,overlap_WB);        
        axes(graphicPanel2);
        imagesc(T,F,BA);            
        
        xpp=['Time in Seconds; fs=',num2str(fs),' samples/second'];
        axis xy,xlabel(xpp),ylabel('Frequency in Hz');
        axis([0 length(curr_file)/fs 0 fs/2]);
        axis tight;
        title('Wideband Spectrogram -- Original Signal');  
 end

% Callback for button4 -- define N, the number of samples of delay, derived
% from Nm button
 function button4Callback(h,eventdata)
     Nm=str2num(get(button4,'string'));
     N=round(Nm*fs/1000);
 end

% Callback for button5 -- define alpha, the gain of the delay path
 function button5Callback(h,eventdata)
     alpha=str2num(get(button5,'string'));
 end

% Callback for button6 -- echo signal using delay N samples with gain alpha
% play out echoed file and plot spectrograms of original (padded with N
% zeros) and echoed files
 function button6Callback(h,eventdata)
     button5Callback(h,eventdata);
     button4Callback(h,eventdata);
     yout=echo_file(curr_file,N,alpha);
     ymax=max(max(yout),-min(yout));
     yout=yout*0.9/ymax;

% play out echoed file
     soundsc(yout,fs);
     
% gray scale map
	t=colormap(gray);
	colormap(1-t);

% plot wideband spectrogram on graphics Panel 1
        [B,BA,F,T]=create_spectrogram(yout,nfft_WB,fs,w_WB,dyn_range,logLinear_index,overlap_WB);        
        axes(graphicPanel1);
        imagesc(T,F,BA);            
        
        axis xy,xlabel(xpp),ylabel('Frequency in Hz');
        axis([0 length(curr_file)/fs 0 fs/2]);
        axis tight;
        title('Wideband Spectrogram -- Echoed signal');
        
% re-plot wideband spectrogram on graphics Panel 2
        axes(graphicPanel2);cla;
        ye=[curr_file; zeros(N,1)];
        [B,BA,F,T]=create_spectrogram(ye,nfft_WB,fs,w_WB,dyn_range,logLinear_index,overlap_WB);        
        imagesc(T,F,BA);    
        
        axis xy,xlabel(xpp),ylabel('Frequency in Hz');
        axis([0 length(curr_file)/fs 0 fs/2]);
        axis tight;
        title('Wideband Spectrogram -- Original Signal');
        
% set up title box
    stitle1=strcat('Echo File -- ',file_info_string);
    set(titleBox1,'String',stitle1);
    set(titleBox1,'FontSize',25);
 end

% Callback for button7 -- play sequence of original-echoed-original files
    function button7Callback(h,eventdata)
        
% play out original file followed by echoed file followed by original files
    soundsc(curr_file,fs);
    pause(length(curr_file)/fs);
    soundsc(yout,fs);
    pause(length(curr_file)/fs);
    soundsc(curr_file,fs);
    end

% Callback for button8 -- define output file for echoed signal
 function button8Callback(h,eventdata)
     fileName=get(button8,'string');
 end

% Callback for button9 -- save echoed file in directory that user selects
% using uigetdir command
 function button9Callback(h,eventdata)
     button8Callback(h,eventdata)
     newFolder=uigetdir(cd);
     oldFolder=cd(newFolder);
     % currentDir=pwd;
     % currDir=strcat(currentDir,char,writefilename);
     % wavwrite(yout,fs,currDir);
     wavwrite(yout,fs,16,fileName);
     cd(oldFolder);
 end

% Callback for button10 -- close GUI
 function button10Callback(h,eventdata)
     fclose('all');
     close(gcf);
 end

% create spectrogram function
    function [B,BA,F,T]=create_spectrogram(y,nfft,fs,w,dyn_range,logLinear_index,overlap_new)
        %windowoverlap=fix(0.9*windowwb);
 %       [B,F,T]=spectrogram(y,w,overlap_new,nfft,fs,'yaxis');
[B,F,T]=spectrogram(y,nfft,fs,w,overlap_new);
        BA=[];
        if (logLinear_index == 1)
            BA=20*log10(abs(B));
            BAM=max(BA);
            BAmax=max(BAM);
            BA(find(BA < BAmax-dyn_range))=BAmax-dyn_range;
            
        end
    end
end

Contact us