image thumbnail

Understanding DELTA and ADAPTIVE DELTA MODULATIONS

by

 

A graphical interface to understand the functioning of delta modulation.

modula_delta.m
function varargout = modula_delta(varargin)
% MODULA_DELTA M-file for modula_delta.fig
%      MODULA_DELTA, by itself, creates a new MODULA_DELTA or raises the
%      existing
%      singleton*.
%
%      H = MODULA_DELTA returns the handle to a new MODULA_DELTA or the
%      handle to
%      the existing singleton*.
%
%      MODULA_DELTA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MODULA_DELTA.M with the given input arguments.
%
%      MODULA_DELTA('Property','Value',...) creates a new MODULA_DELTA or raises the
%      existing singleton*.  Starting from the left, property value pairs
%      are
%      applied to the GUI before modula_delta_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to modula_delta_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 modula_delta
% powered by Pedro S de Melo and Hlio Magalhes de Oliveira, UFPE, 2010
% Last Modified by GUIDE v2.5 09-Apr-2013 14:24:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @modula_delta_OpeningFcn, ...
                   'gui_OutputFcn',  @modula_delta_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 modula_delta is made visible.
function modula_delta_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 modula_delta (see VARARGIN)

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

scrsz = get(0,'Screensize');
pos = get(handles.figure1,'Position');
set(handles.figure1,'Position',[(scrsz(3) - pos(3))/2 (scrsz(4) - pos(4))/2 pos(3) pos(4) ],'Units','pixels')

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = modula_delta_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;

clear global sinal_modulado;
clear global audio;
clear global fS;
clear global tempo_delta;
clear global num_amostras;
clear global freq_delta;
clear global filtrado;
clear global arq_anterior;



function arq_audio_Callback(hObject, eventdata, handles)
% hObject    handle to arq_audio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of arq_audio as text
%        str2double(get(hObject,'String')) returns contents of arq_audio as a double


% --- Executes during object creation, after setting all properties.
function arq_audio_CreateFcn(hObject, eventdata, handles)
% hObject    handle to arq_audio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in botao_abrir.
function botao_abrir_Callback(hObject, eventdata, handles)
% hObject    handle to botao_abrir (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] = uigetfile('*.wav','Select an audio file');
if (FileName ~=0)
    set(handles.arq_audio,'String',[PathName FileName]);
end



function freq_delta_Callback(hObject, eventdata, handles)
% hObject    handle to freq_delta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    user_entry = str2double(get(hObject,'string'));
    if isnan(user_entry)
             errordlg('must be numerical!','Bad Input','modal')
             set(hObject,'String','50');
    end

% Hints: get(hObject,'String') returns contents of freq_delta as text
%        str2double(get(hObject,'String')) returns contents of freq_delta as a double


% --- Executes during object creation, after setting all properties.
function freq_delta_CreateFcn(hObject, eventdata, handles)
% hObject    handle to freq_delta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function amp_delta_Callback(hObject, eventdata, handles)
% hObject    handle to amp_delta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    user_entry = str2double(get(hObject,'string'));
    if isnan(user_entry)
             errordlg('must be numerical!','Bad Input','modal')
             set(hObject,'String','0.1');
    end

% Hints: get(hObject,'String') returns contents of amp_delta as text
%        str2double(get(hObject,'String')) returns contents of amp_delta as a double


% --- Executes during object creation, after setting all properties.
function amp_delta_CreateFcn(hObject, eventdata, handles)
% hObject    handle to amp_delta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checa_adap.
function checa_adap_Callback(hObject, eventdata, handles)
% hObject    handle to checa_adap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checa_adap


% --- Executes on button press in checa_filtr.
function checa_filtr_Callback(hObject, eventdata, handles)
% hObject    handle to checa_filtr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checa_filtr


% --- Executes on button press in checa_ruido.
function checa_ruido_Callback(hObject, eventdata, handles)
% hObject    handle to checa_ruido (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checa_ruido


% --- Executes on selection change in lista_adap.
function lista_adap_Callback(hObject, eventdata, handles)
% hObject    handle to lista_adap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns lista_adap contents as cell array
%        contents{get(hObject,'Value')} returns selected item from lista_adap


% --- Executes during object creation, after setting all properties.
function lista_adap_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lista_adap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function freq_inf_Callback(hObject, eventdata, handles)
% hObject    handle to freq_inf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    user_entry = str2double(get(hObject,'string'));
    if isnan(user_entry)
             errordlg('must be numerical!','Bad Input','modal')
             set(hObject,'String','300');
    end

% Hints: get(hObject,'String') returns contents of freq_inf as text
%        str2double(get(hObject,'String')) returns contents of freq_inf as a double


% --- Executes during object creation, after setting all properties.
function freq_inf_CreateFcn(hObject, eventdata, handles)
% hObject    handle to freq_inf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function freq_sup_Callback(hObject, eventdata, handles)
% hObject    handle to freq_sup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
user_entry = str2double(get(hObject,'string'));
if isnan(user_entry)
         errordlg('must be numerical!','Bad Input','modal')
         set(hObject,'String','3600');
end

% Hints: get(hObject,'String') returns contents of freq_sup as text
%        str2double(get(hObject,'String')) returns contents of freq_sup as a double


% --- Executes during object creation, after setting all properties.
function freq_sup_CreateFcn(hObject, eventdata, handles)
% hObject    handle to freq_sup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in botao_modula.
function botao_modula_Callback(hObject, eventdata, handles)
% hObject    handle to botao_modula (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global sinal_modulado;
global audio;
global fS;
global tempo_delta;
global num_amostras;
global freq_delta;
global filtrado;
global arq_anterior;


arq_audio = get(handles.arq_audio,'String');


freq_delta = 1e3 * str2double(get(handles.freq_delta,'string'));
p_erro = str2double(get(handles.p_erro,'string'));
f_inf = str2double(get(handles.freq_inf,'string'));
f_sup = str2double(get(handles.freq_sup,'string'));

if (get(handles.radio_Nadap,'Value') == get(handles.radio_Nadap,'Max'))
    amp_delta = str2double(get(handles.amp_delta,'string'));
else
    amp_delta = str2double(get(handles.lista_adap,'string'));
end

try
    [audio,fS] = wavread(arq_audio);
catch
    errordlg('missed audio file.');
    return
end

audio = mean(audio,2);
num_amostras = length(audio);

num_amostras_delta = floor(freq_delta/fS * num_amostras);
sinal_modulado = zeros(num_amostras_delta,1);
mod_delta_bin = zeros(num_amostras_delta-2,1);
tempo_delta = zeros(2*num_amostras_delta-2,1);

razao_freq = fS/freq_delta;

pos_neg = 1;
indice = 1;
    
for i=1:num_amostras_delta-2
    % Interpolao do sinal de udio
    int_rf = floor(i*razao_freq);
    frac_rf = i*razao_freq - int_rf;

    audio_interpol = (1-frac_rf) * audio(int_rf+1) + frac_rf * audio(int_rf+2);
    
    %Modulao
    if (audio_interpol > sinal_modulado(i))
        if (pos_neg == -1)
            pos_neg = 1;
            indice = 1;
        end
        sinal_modulado(i+1) = sinal_modulado(i) + amp_delta(indice);
        mod_delta_bin(i) = 1;
        
        if (indice < length(amp_delta))
            indice = indice + 1;
        end
        
    else
        if (pos_neg == 1)
            pos_neg = -1;
            indice = 1;
        end
        sinal_modulado(i+1) = sinal_modulado(i) - amp_delta(indice);
        mod_delta_bin(i) = 0;
        
        if (indice < length(amp_delta))
            indice = indice + 1;
        end
    end
end


for i=1:num_amostras_delta-1
    tempo_delta(2*i-1) = (i-1);
    tempo_delta(2*i) = (i-1e-6);
end


X = get(handles.graf_delta,'XLim');
Y = get(handles.graf_delta,'YLim');

set(handles.popupmenu_plot,'Enable','on');
if (get(handles.checa_ruido,'Value') == get(handles.checa_ruido,'Max'))
    delta_ruido = mod(mod_delta_bin + round(.5/(1-p_erro) * rand(length(mod_delta_bin),1)),2);
    
    indice = 1;
    sinal_modulado = zeros(num_amostras_delta-1,1);
    
    for i=1:num_amostras_delta-3
        sinal_modulado(i+1) = sinal_modulado(i) + 2*(delta_ruido(i) - 0.5) * amp_delta(indice);
        
        if (indice < length(amp_delta))
            indice = indice + 1;
        end
        if (delta_ruido(i+1) ~= delta_ruido(i))
            indice = 1;
        end
    end
    
    if (get(handles.checa_filtr,'Value') == get(handles.checa_ruido,'Max'))
        muda_string_popup(1,handles);
        
        [a,b] = butter(6,[2*f_inf/freq_delta,2*f_sup/freq_delta]);
        filtrado = filter(a,b,sinal_modulado);
        wavplay(filtrado,freq_delta);
    else
        muda_string_popup(0,handles);
        wavplay(sinal_modulado,freq_delta);
    end
    plotar(handles);
    
else
    if (get(handles.checa_filtr,'Value') == get(handles.checa_ruido,'Max'))
        
        [a,b] = butter(6,[2*f_inf/freq_delta,2*f_sup/freq_delta]);
        filtrado = filter(a,b,sinal_modulado);
        wavplay(filtrado,freq_delta);
        muda_string_popup(1,handles);
    else
        muda_string_popup(0,handles);
        wavplay(sinal_modulado,freq_delta);
    end
    plotar(handles);
end

if (strcmp(arq_audio,arq_anterior) == 1)
    set(handles.graf_delta,'XLim',X);
    set(handles.graf_delta,'YLim',Y);
end
arq_anterior = arq_audio;


    
function muda_string_popup(filtrado,handles)
if (filtrado)
    set(handles.popupmenu_plot,'String',{'Time domain',...
            'Spectrum - Original + Delta',...
            'Spectrum - Original + filtered Delta'});
else
    set(handles.popupmenu_plot,'String',{'Time Domain',...
            'Spectrum - Original + Delta'});
        if (get(handles.popupmenu_plot,'Value') == 3)
            set(handles.popupmenu_plot,'Value',2)
        end
end
    


function p_erro_Callback(hObject, eventdata, handles)
% hObject    handle to p_erro (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    user_entry = str2double(get(hObject,'string'));
    if isnan(user_entry)
             errordlg('must be numerical!','Bad Input','modal')
             set(hObject,'String','0');
    else if(user_entry < 0 || user_entry > 0.5)
             errordlg('The value should be in the range 0 -- 0.5.','Bad Input','modal')
             set(hObject,'String','0');
        end
    end

% Hints: get(hObject,'String') returns contents of p_erro as text
%        str2double(get(hObject,'String')) returns contents of p_erro as a double


% --- Executes during object creation, after setting all properties.
function p_erro_CreateFcn(hObject, eventdata, handles)
% hObject    handle to p_erro (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in adicionar.
function adicionar_Callback(hObject, eventdata, handles)
% hObject    handle to adicionar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

lista = get(handles.lista_adap,'String');
if (~isempty(lista))
    ultimo_lista = lista{length(lista)};
else
    ultimo_lista = '0.1';
end

amp_adap = inputdlg({'Insert an amplitude for the adaptative delta step'},'Adaptive Delta',1,{ultimo_lista});

if isnan(str2double(amp_adap))
         errordlg('must be numerical!','Bad Input','modal')
         return
end

valores = get(handles.lista_adap,'String');
set(handles.lista_adap,'String',[valores;amp_adap]);
set(handles.lista_adap,'Value',length(get(handles.lista_adap,'String')));




% --- Executes on button press in apagar.
function apagar_Callback(hObject, eventdata, handles)
% hObject    handle to apagar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

valores = get(handles.lista_adap,'String');
selecionado = get(handles.lista_adap,'Value');

if (selecionado == length(valores) && selecionado > 1)
    set(handles.lista_adap,'Value',selecionado-1);
end

set(handles.lista_adap,'String',[valores(1:selecionado-1);valores(selecionado+1:length(valores))]);



function grupo_adap_SelectionChangeFcn(hObject,eventdata,handles)
switch get(hObject,'Tag')   % Get Tag of selected object
    case 'radio_Nadap'
        set(handles.text_adaptativo,'ForegroundColor',[0 0 0]);
        set(handles.amp_delta,'Enable','on');
        set(handles.lista_adap,'Enable','off');
        set(handles.adicionar,'Enable','off');
        set(handles.apagar,'Enable','off');
    case 'radio_adap'
        set(handles.text_adaptativo,'ForegroundColor',[.6 .6 .6]);
        set(handles.amp_delta,'Enable','off');
        set(handles.lista_adap,'Enable','on');
        set(handles.adicionar,'Enable','on');
        set(handles.apagar,'Enable','on');
    otherwise
        % Code for when there is no match.
end


% --- Executes on selection change in popupmenu_plot.
function popupmenu_plot_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

plotar(handles);

        

function plotar(handles)
global sinal_modulado;
global audio;
global fS;
global tempo_delta;
global num_amostras;
global freq_delta
global filtrado;

opcoes = get(handles.popupmenu_plot,'Value');
lista = get(handles.popupmenu_plot,'String');

switch(lista{opcoes})
    case 'Time Domain'
        set(handles.eixo_x,'String','Time (s)');
        plot(handles.graf_delta,(0:num_amostras-1)/fS,audio,tempo_delta/freq_delta,sinal_modulado(floor(tempo_delta+1)));
        
        zoom(0,'reset')
        
        legend('original audio','delta modulated audio');
    case 'Spectrum - Original + Delta'
        set(handles.eixo_x,'String','Frequency (Hz)');
        
        comp_modulado = length(sinal_modulado);
        fft_audio = abs(fft(audio))/num_amostras;
        fft_modul = abs(fft(sinal_modulado))/comp_modulado;
        
        omega_audio = (0:floor(num_amostras/2)-1)/(num_amostras/2) * fS/2;
        omega_delta = (0:floor(comp_modulado/2)-1)/(comp_modulado/2) * freq_delta/2;
        
        
        plot(handles.graf_delta,omega_audio,fft_audio(1:floor(num_amostras/2)),omega_delta,fft_modul(1:floor(comp_modulado/2)))
        zoom(0,'reset')
            
        legend('original audio','delta modulated signal');
        
    case 'Spectrum - Original + filtered Delta (DM)'
        set(handles.eixo_x,'String','Frequency (Hz)');
        
        comp_modulado = length(sinal_modulado);
        fft_audio = abs(fft(audio))/num_amostras;
        fft_filtr = abs(fft(filtrado))/comp_modulado;
        
        omega_audio = (0:floor(num_amostras/2)-1)/(num_amostras/2) * fS/2;
        omega_delta = (0:floor(comp_modulado/2)-1)/(comp_modulado/2) * freq_delta/2;
        
        
        plot(handles.graf_delta,omega_audio,fft_audio(1:floor(num_amostras/2)),omega_delta,fft_filtr(1:floor(comp_modulado/2)));
        zoom(0,'reset')
        
        legend('original audio signal','(filtered) delta modulated audio');

end

    

% Hints: contents = get(hObject,'String') returns popupmenu_plot contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_plot


% --- Executes during object creation, after setting all properties.
function popupmenu_plot_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

Contact us