]> git.parisson.com Git - cnaq.git/commitdiff
* Externalize main functions to tools/
authoryomguy <yomguy@5fc3e0e6-29bc-4d03-b52b-c088cb822bde>
Thu, 27 Mar 2008 11:27:35 +0000 (11:27 +0000)
committeryomguy <yomguy@5fc3e0e6-29bc-4d03-b52b-c088cb822bde>
Thu, 27 Mar 2008 11:27:35 +0000 (11:27 +0000)
git-svn-id: http://svn.parisson.org/svn/CNAQ/trunk@154 5fc3e0e6-29bc-4d03-b52b-c088cb822bde

15 files changed:
tools/generator.m [new file with mode: 0644]
tools/get_fs.m [new file with mode: 0644]
tools/get_id.m [new file with mode: 0644]
tools/get_nbits.m [new file with mode: 0644]
tools/get_version.m [new file with mode: 0644]
tools/get_voices_in.m [new file with mode: 0644]
tools/get_voices_out.m [new file with mode: 0644]
tools/increment_id.m [new file with mode: 0644]
tools/load_mes.m [new file with mode: 0644]
tools/measurement.m [new file with mode: 0644]
tools/monitor.m [new file with mode: 0644]
tools/plot_main.m [new file with mode: 0644]
tools/save_mes.m [new file with mode: 0644]
tools/set_fs.m [new file with mode: 0644]
tools/set_nbits.m [new file with mode: 0644]

diff --git a/tools/generator.m b/tools/generator.m
new file mode 100644 (file)
index 0000000..a8b7603
--- /dev/null
@@ -0,0 +1,56 @@
+function generator(handles)
+
+    f_min = str2double(get(handles.f_gen_min,'String'));
+    f_max = str2double(get(handles.f_gen_max,'String'));
+    sig_type = get(handles.sig_type,'Value');
+    sin_freq = get(handles.f_gen,'Value');
+    f_s = get_fs(handles);
+    time = get(handles.time_gen,'Value');
+    voices_out = get_voices_out(handles);
+    %voice_first = voices_out(1);
+    %voice_last = voices_out(length(voices_out));
+    gain_out = get(handles.gain_out,'Value');
+    gain_out = 10^(gain_out/20);
+    t = [0:1/f_s:time];
+    n_t = length(t);
+    
+    if sig_type == 1
+        % SINUS
+        sig = gain_out*sin(2*pi*sin_freq*t);
+    elseif sig_type == 2
+        % CHIRP
+        sig = gain_out*chirp(t,f_min,time,f_max,'logarithmic');
+    elseif sig_type == 3
+        % WHITE NOISE
+        sig = gain_out*white_noise(n_t);
+    elseif sig_type == 4
+        % PINK NOISE
+        sig = gain_out*pink_noise(n_t);
+    end
+  
+    sig_out = [];
+    for i=1:length(voices_out)
+        sig_out(:,i) = sig';
+    end
+
+    % Matlab way (needs Data Acquisition Toolbox)
+%      ao = analogoutput('winsound', 0);
+%      addchannel(ao, voices_out);
+%      set(ao, 'StandardSampleRates', 'Off');
+%      set(ao, 'SampleRate', f_s);
+%      
+%      if get(handles.gen_on_off,'Value') == 1
+%          putdata(ao, sig_out);
+%          start(ao);
+%          set(handles.gen_on_off,'Value',0);
+%      end
+%      %delete(ao);
+
+    % PA way but can't work with monitor !...
+    %pa_wavplay(sig_out',f_s,0,'asio');
+    
+    % Winsoud way to get the scope running...
+    sound(sig_out',f_s);
+    pause(0.1);
+
+end
diff --git a/tools/get_fs.m b/tools/get_fs.m
new file mode 100644 (file)
index 0000000..0f67ee2
--- /dev/null
@@ -0,0 +1,12 @@
+function f_s = get_fs(handles)
+
+    fs_ind = get(handles.f_s,'Value');
+    if fs_ind == 1
+        f_s = 44100;
+    elseif fs_ind == 2
+        f_s = 48000;
+    elseif fs_ind == 3
+        f_s = 96000;
+    end
+
+end
\ No newline at end of file
diff --git a/tools/get_id.m b/tools/get_id.m
new file mode 100644 (file)
index 0000000..05c2678
--- /dev/null
@@ -0,0 +1,13 @@
+function id = get_id(handles)
+
+    home_dir = get(handles.home_dir_box,'String');
+    id_file = [home_dir '\ID.m'];
+    if exist(id_file) == 0
+        id = '1';
+    elseif exist(id_file) == 2
+        fid = fopen(id_file, 'r');
+        id = fgetl(fid);
+        fclose(fid); 
+    end
+
+end
\ No newline at end of file
diff --git a/tools/get_nbits.m b/tools/get_nbits.m
new file mode 100644 (file)
index 0000000..e85ce0f
--- /dev/null
@@ -0,0 +1,10 @@
+function nbits = get_nbits(handles)
+
+    nb_ind = get(handles.nbits,'Value');
+    if nb_ind == 1
+        nbits = 16;
+    elseif nb_ind == 2
+        nbits = 24;
+    end
+
+end
\ No newline at end of file
diff --git a/tools/get_version.m b/tools/get_version.m
new file mode 100644 (file)
index 0000000..09c6198
--- /dev/null
@@ -0,0 +1,2 @@
+function version = get_version()
+    version = '0.2'
diff --git a/tools/get_voices_in.m b/tools/get_voices_in.m
new file mode 100644 (file)
index 0000000..33e1eab
--- /dev/null
@@ -0,0 +1,14 @@
+function voices_in = get_voices_in(handles)
+
+    vi_ind = get(handles.voices_in,'Value');
+    if vi_ind == 1
+       voices_in = [1];
+    elseif vi_ind == 2
+       voices_in = [1 2];
+    elseif vi_ind == 3
+       voices_in = [1 2 3];
+    elseif vi_ind == 4
+       voices_in = [1 2 3 4];
+    end
+
+end
\ No newline at end of file
diff --git a/tools/get_voices_out.m b/tools/get_voices_out.m
new file mode 100644 (file)
index 0000000..0dd1f4f
--- /dev/null
@@ -0,0 +1,14 @@
+function voices_out = get_voices_out(handles)
+
+    vo_ind = get(handles.voices_out,'Value');
+    if vo_ind == 1
+       voices_out  = [1];
+    elseif vo_ind == 2
+       voices_out = [1,2];
+    elseif vo_ind == 3
+       voices_out = [1,2,3];
+    elseif vo_ind == 4
+       voices_out = [1,2,3,4];
+    end
+
+end
\ No newline at end of file
diff --git a/tools/increment_id.m b/tools/increment_id.m
new file mode 100644 (file)
index 0000000..c324ceb
--- /dev/null
@@ -0,0 +1,12 @@
+function increment_id(handles)
+
+    home_dir = get(handles.home_dir_box,'String');
+    id_file = [home_dir '\ID.m'];
+    id = get_id(handles);
+    id = num2str(str2double(id) + 1);
+    fid = fopen(id_file, 'w+');
+    fprintf(fid, id);
+    fclose(fid);
+    set(handles.ID,'String',id);
+
+end
\ No newline at end of file
diff --git a/tools/load_mes.m b/tools/load_mes.m
new file mode 100644 (file)
index 0000000..8908e6a
--- /dev/null
@@ -0,0 +1,29 @@
+function load_mes(handless)
+
+    [filename, pathname, filterindex] = uigetfile('*.mat', 'Pick a saved MAT file');
+    load([pathname filename]);
+    
+    % Set data
+    set(handless.username,'String',username);
+    set(handless.home_dir_box,'String',home_dir);
+    set(handless.comment,'String',comment);
+    set(handless.id_title,'UserData',sig_exc);
+    set(handless.ID,'UserData',sig_mes);
+    set(handless.mes_on,'UserData',f_log);
+    set(handless.close_button,'UserData',f_lin);
+    set_fs(handless, f_s);
+    set_nbits(handless, nbits);
+    set(handless.mes_on,'UserData', f);
+    set(handless.plot,'UserData', t);
+    set(handless.f_gen_min,'String', num2str(f_min));
+    set(handless.f_gen_max,'String', num2str(f_max));
+    set(handless.f_gen,'Value', f);
+    set(handless.freq_value,'String',num2str(f));
+    set(handless.time_gen,'Value', time);
+    set(handless.time_value,'String',num2str(time));
+    set(handless.gain_in,'Value', gain_in);
+    set(handless.gain_out,'Value', gain_out);
+    set(handless.gain_in_value,'String',num2str(gain_in));
+    set(handless.gain_out_value,'String',num2str(gain_out));
+    
+end
\ No newline at end of file
diff --git a/tools/measurement.m b/tools/measurement.m
new file mode 100644 (file)
index 0000000..2b8f2bb
--- /dev/null
@@ -0,0 +1,79 @@
+function measurement(handles)
+
+    pause(0.2);
+    device = get(handles.in_on_off,'UserData');
+    latency = get(handles.save_button,'UserData');
+    nfft = 32768;
+    f_min = str2double(get(handles.f_gen_min,'String'));
+    f_max = str2double(get(handles.f_gen_max,'String'));
+    f_s = get_fs(handles);
+    time = get(handles.time_gen,'Value');
+    voices_in = get_voices_in(handles);
+    voice_first = voices_in(1);
+    voice_last =  voices_in(length(voices_in));
+    gain_out = get(handles.gain_out,'Value');
+    gain_out = 10^(gain_out/20);
+    
+    % Avoid Gibbs like phenomenon
+    f0 = 1;
+    f1 = f_min;
+    f2 = f_max;
+    f3 = f_s/2;
+    fade_in_time = time/((log(f2/f0)/log(f1/f0))-1);
+    fade_out_time = time*((log(f3/f1)/log(f2/f1))-1);  
+    total_time = fade_in_time + time + fade_out_time;
+    t = [0:1/f_s:total_time];
+    
+    % Remove clicks durring emission and oscillations in the spectral response
+    len_win_in = fade_in_time * f_s;
+    window = blackman(len_win_in);
+    len_win_in = round(len_win_in/2);
+    window_in = window(1:len_win_in);
+    
+    len_win_out = fade_out_time * f_s;
+    window = blackman   (len_win_out);
+    len_win_out = round(len_win_out/2);
+    window_out = flipud(window(1:len_win_out));
+    
+    sig_exc = gain_out*chirp_farina(t,total_time,f0,f3);
+    l_t = length(t);
+    one(1:l_t-len_win_in-len_win_out) = 1;
+    mask = [window_in' one window_out'];
+    sig_exc = sig_exc.*mask;
+    
+    % Synchronizing
+    zero = zeros(1,latency);
+    % Zeros are added before and removed after
+    sig_exc_z = [sig_exc zero];
+    len_sig_exc = length(sig_exc);
+    
+    % Make all voices
+    sig_out = [];
+    for i=1:length(voices_in)
+        sig_out(:,i) = sig_exc_z';
+    end
+    
+    % Measure
+    sig_mes = pa_wavplayrecord(sig_out, device, f_s, 0, voice_first, voice_last, device, 'asio');
+    %    Usage:
+    %      inputbuffer = pa_wavplayrecord(playbuffer,[playdevice],[samplerate],
+    %                       [recnsamples], [recfirstchannel], [reclastchannel],
+    %                       [recdevice], [devicetype])
+    
+    % Resynchro
+    pause(0.1);
+    len_sig_mes = length(sig_mes);
+    size_sig_mes = size(sig_mes);
+    n_col_sig_mes = size_sig_mes(2);
+    sig_mes = sig_mes(latency+1:len_sig_mes,:);
+    len_sig_mes = length(sig_mes);
+    sig_exc = sig_exc';
+    f = logspace(log10(f0), log10(f3), len_sig_mes)';
+    
+    % Save data
+    set(handles.ID,'UserData',sig_mes);
+    set(handles.id_title,'UserData',sig_exc);
+    set(handles.mes_on,'UserData',f);
+    set(handles.plot,'UserData',t);
+    
+end
diff --git a/tools/monitor.m b/tools/monitor.m
new file mode 100644 (file)
index 0000000..16c716d
--- /dev/null
@@ -0,0 +1,43 @@
+function monitor(handles)
+
+    device = get(handles.in_on_off,'UserData');
+    buffer = 8192;
+    window = hanning(buffer);
+    f_s = get_fs(handles);
+    time = buffer/f_s;
+    t = [0:1/f_s:time-1/f_s];
+    
+    f_min = str2double(get(handles.f_gen_min,'String'));
+    f_max = str2double(get(handles.f_gen_max,'String'));
+    freq = 2*(f_max-f_min)/buffer;
+    f = [0:f_s/buffer:f_s/2];
+    f = f(1:length(f)-1);
+    voices_in = get_voices_in(handles);
+    voice_first = voices_in(1);
+    voice_last =  voices_in(length(voices_in));
+        
+    while get(handles.in_on_off,'Value') == 1
+        % TIME_GEN
+        %sig_in = wavrecord(buffer,f_s,2);
+        sig_in = pa_wavrecord(voice_first, voice_last, buffer, f_s, device, 'asio');
+        sig_in = sig_in(:,1);
+        axes(handles.plot_in_temp);
+        cla;
+        %sig = sin(2*pi*10*t);
+        plot(t,sig_in);
+        grid on;
+        
+        % FREQ
+        axes(handles.plot_in_freq);
+        cla;
+        fft_in = fft(sig_in.*window,buffer);
+        log_abs_fft_in = 20*log10(2*abs(fft_in(1:round(buffer/2)))/buffer);
+        semilogx(f,log_abs_fft_in);
+        %axis([f_min f_max min(log_abs_fft_in) max(log_abs_fft_in)]);
+        axis([f_min f_max -120 0]);
+        grid on;
+        drawnow;
+        pause(0.01);
+    end
+
+end
\ No newline at end of file
diff --git a/tools/plot_main.m b/tools/plot_main.m
new file mode 100644 (file)
index 0000000..179105e
--- /dev/null
@@ -0,0 +1,40 @@
+function plot_main(handles)
+
+    % Get data
+    sig_mes = get(handles.ID,'UserData');
+    sig_exc = get(handles.id_title,'UserData');
+    f = get(handles.mes_on,'UserData');
+    t = get(handles.plot,'UserData');
+    f_min = str2double(get(handles.f_gen_min,'String'));
+    f_max = str2double(get(handles.f_gen_max,'String'));
+    f_s = get_fs(handles);
+    
+    % Get infos
+    username = get(handles.username,'String');
+    comment = get(handles.comment,'String');
+    id = get(handles.ID,'String');
+    
+    % Compute excitation spectrum
+    [rep_imp_exc, spec_exc] = fonc_trans(f, sig_exc, sig_exc);
+    len_spec_exc = length(spec_exc);
+    spec_exc = spec_exc(1:len_spec_exc/2);
+    
+    size_sig_mes = size(sig_mes);
+    n_col_sig_mes = size_sig_mes(2);
+    
+    % Compute all Ris and specs
+    for i=1:n_col_sig_mes
+        voice = num2str(i);
+        [rep_imp_mes, spec_mes] = fonc_trans(f, sig_exc, sig_mes(:,i));
+        len_spec_mes = length(spec_mes);    
+        spec_mes = spec_mes(1:len_spec_mes/2);
+        % Plot results
+        f_lin = [0:f_s/len_spec_mes:f_s/2];
+        f_lin = f_lin(1:length(f_lin)-1);
+        plot_mes(t, f_lin, f_s, f_min, f_max, sig_exc, sig_mes(:,i), rep_imp_mes, spec_mes, spec_exc, id, voice, username, comment, i);
+    end
+
+    set(handles.close_button,'UserData',f_lin');
+    set(handles.plot,'UserData',t);
+
+end
diff --git a/tools/save_mes.m b/tools/save_mes.m
new file mode 100644 (file)
index 0000000..7afb30e
--- /dev/null
@@ -0,0 +1,36 @@
+function save_mes(handles)
+
+    % Get data
+    id = get(handles.ID,'String');
+    username = get(handles.username,'String');
+    home_dir = get(handles.home_dir_box,'String');
+    comment = get(handles.comment,'String');
+    sig_exc = get(handles.id_title,'UserData');
+    sig_mes = get(handles.ID,'UserData');
+    f_log = get(handles.mes_on,'UserData');
+    f_lin = get(handles.close_button,'UserData');
+    f_s = get_fs(handles);
+    nbits = get_nbits(handles);
+    f = get(handles.mes_on,'UserData');
+    t = get(handles.plot,'UserData');
+    f_min = str2double(get(handles.f_gen_min,'String'));
+    f_max = str2double(get(handles.f_gen_max,'String'));
+    time = get(handles.time_gen,'Value');
+    voices_in = get_voices_in(handles);
+    gain_in = get(handles.gain_in,'Value');
+    voices_out = get_voices_out(handles);
+    gain_out = get(handles.gain_out,'Value');
+    
+    % Save it
+    file = [home_dir '\' username '_' id '.mat'];
+    save(file);
+    
+    % Clear big data
+    set(handles.ID,'UserData',[]);
+    set(handles.id_title,'UserData',[]);
+    set(handles.mes_on,'UserData',[]);
+    
+    % Increment ID
+    increment_id(handles);
+
+end
\ No newline at end of file
diff --git a/tools/set_fs.m b/tools/set_fs.m
new file mode 100644 (file)
index 0000000..61a1b97
--- /dev/null
@@ -0,0 +1,17 @@
+function set_fs(handles, f_s)
+
+    if f_s == 44100
+        fs_ind = 1;
+    elseif f_s == 48000
+        fs_ind = 2;
+    elseif f_s == 88200
+        fs_ind = 3;
+    elseif f_s == 96000
+        fs_ind = 4;
+    elseif f_s == 192000
+        fs_ind = 5;
+    end
+
+    set(handles.f_s,'Value',fs_ind);
+
+end
\ No newline at end of file
diff --git a/tools/set_nbits.m b/tools/set_nbits.m
new file mode 100644 (file)
index 0000000..c8555ae
--- /dev/null
@@ -0,0 +1,10 @@
+function set_nbits(handles, nbits)
+
+    if nbits == 16
+        nb_ind = 1;
+    elseif nbits == 24
+        nb_ind = 2;
+    end
+    set(handles.nbits,'Value', nb_ind);
+
+end
\ No newline at end of file