Commit 3155b6a3 authored by Johannes Klein's avatar Johannes Klein

Cleanup and revamp tutorials

parent 937a1d00
%% bung zur Vorlesung Elektroakustik - PDI, JCK
%Init
% <ITA-Toolbox>
% This file is part of the application Laboratory for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>
ccx;
ita_preferences('toolboxlogo',false);
%% Introduction to Discrete Fourier Transform
% Generate a simple sine and plot the time signal.
% Task: Use the menu bar to see the spectrum (domain->magnitude)
f = 1000; % Sine frequency in Hz.
amplitude = 1; % Amplitude.
fftDegree = 14; % Signal length, see explanation below.
sr = 44100; % Sampling rate in 1/s.
% fftDegree: For the efficient fast Fourier transform (FFT) a signal with a
% number of samples in the power of two is required. Thus the
% signal lengths are varied in steps.
% Example:
% - Sampling rate: 44100 1/s
%
% - Number of samples: 2^17 = 131072
% - Resulting signal length: 2^17/44100Hz = 2.9722 s
%
% - Number of samples: 2^18 = 262144
% - Resulting signal length: 2^18/44100 = 5.9443 s
% Generate the signal
sine = ita_generate('sine',amplitude,f,sr,fftDegree);
sine.nSamples = 890;
sine.plot_time % Plot resulting sine signal in the time domain. Reminder: Take a look at the frequency domain!
%% Audio Signal Processing with Music Signal
% Open a music file. This could also be a measurement or recording.
s = ita_demosound;
s.plot_time
% play back the signal
s.play;
s.plot_freq
%% view the spectrogram
s.plot_spectrogram
%% Filtering
% Let's apply a low and high pass, that means we actually apply a bandpass, to the
% original music signal. In this case we take the band limitations from a
% telephone line.
%
% * 300 Hz lower cut off
% * 4000 Hz upper cut off
%
f_low = 300;
f_high = 4000;
g = ita_filter_bandpass(s,'lower',f_low,'upper',f_high);
% Have a look at the results
g.plot_freq
% Play back the telephone emulation
g.play
% view the spectrogram
g.plot_spectrogram
%% Influence of the phase;
% Take the music signal and delete the phase information.
close all
g_nophase = ita_zerophase(s);
g_nophase.plot_freq_phase
g_nophase.play
%% Add a random phase but take the original magnitude of the spectrum
g_randphase = ita_randomize_phase( s );
g_randphase.plot_freq_phase;
g_randphase.play;
return %% back to PPT !
%% Generate perfect impulse
close all, clc, fftDegree = 16; sr = 44100;
impulse = ita_generate('impulse',1,sr,fftDegree);
impulse.plot_time('xlim',[-1 2])
impulse.play
%% Deconvolution with Music
s = ita_demosound;
h = fft(s) / fft(s);
h.plot_spkphase
%% Avoid Division by zero
H = ita_divide_spk(s, s, 'regularization',[100 10000]);
H.plot_freq
return %% back to PPT !
%% Measurement Signals -- Generate white noiselevel
close all, clc, fftDegree = 16; sr = 44100;
noise = ita_generate('noise',1,sr,fftDegree);
noise = ita_normalize_dat(noise); % Normalize to 0dBFS
noise.plot_spectrogram
noise.play
%% Generate linear sweep
sweep = ita_generate_sweep('mode','lin','fftDegree',fftDegree, 'samplingRate',sr , 'freqRange', [40 18000]);
sweep.plot_spectrogram
sweep.play
%% Generate exponential sweep
sweep = ita_generate_sweep('fftDegree',fftDegree, 'samplingRate',sr , 'freqRange', [40 18000]);
sweep.plot_spectrogram
sweep.play
return %% back to PPT !
%% Generate artifical room impulse response
% In this section a simulated room impulse response (RIR) will be
% generated basing on a noise signal.
% Set room and recording position parameters.
revTime = 1; % Reverberation time of the room, in sec.
delay = 0.05; % Pre-delay before sound arrives at the listener, in sec.
fftDegree = 17; % length of IR is 2^fftDegree samples
% Generate RIR
RIR = ita_generate('noise', 0.5, sr, fftDegree); % Plain noise
RIR.timeData = RIR.timeData .* 10 .^( RIR.timeVector * ( -60/ revTime ) / 20); % with exponantial decay (=signal)
RIR = ita_time_shift(RIR, delay, 'time'); % and delay (in seconds)
RIR.signalType = 'energy';
RIR.channelNames{1} = 'ideal RIR';
RIR.plot_time_dB
%% Use the Measurement Dummy Class from the ITA-Toolbox for virtual measurement
MS = itaMSTFdummy; % get measurement setup object
% specify measurement parameters
MS.fftDegree = 19; % Set length of sweep signal
MS.stopMargin = 1; % Time to wait until the system decays
MS.freqRange = [20 20000]; % Frequency range for the measurement
MS.outputamplification = -30; % level below maximum amplitude in dBFS (full scale)
MS.averages = 1; % number of measurements to be averaged to get a mean result
MS.samplingRate = sr; % sampling Rate of the system
% specify the device under test (DUT) to be measured
MS.nonlinearCoefficients = [1]; % only linear transmission or e.g. [1 0.1 0.1] for g = 1*s^1 + 0.1*s^2 + 0.1*s^3
MS.noiselevel = -30; % noise level below maximum amplitude in dBFS
MS.systemresponse = RIR; % impulse response of the system
MS.nBits = 24; % Quantization of the system
% simulation of the actual measurement
h_measured = MS.run; % get the measured impulse response with nonlinearities, quantization and noise
h_measured.plot_time_dB % plot IR in dB
%% How to obtain a higher SNR ?
% Usually the noise level during room acoustical measurements is quite
% high. To improve the signal to noise ratio (SNR), we can measure the IR
% several times and average the results. This improves the SNR since noise
% is not correlated (+3dB), but the measurement signal is correlated
% (+6dB).
% Close all plots
close all
% Set number of averages, feel free to experiment!
MS.averages = 4;
% % Average! - this is what happens below, when you run your measurement
% for idx = 1:averages
% awgn = 10^(-SNR / 20) * ita_generate('noise', 1, sr, s.nSamples); % Generate new additive measurement noise for every measurement!
% g(idx) = s*RIR + awgn; % Add background noise
% end
% g = sum(g)/averages;
% Measurement with deconvolution
h_measured_av = MS.run ;
h_measured_av.channelNames{1} = ['with ' num2str(MS.averages) ' averages'];
% Merge
res = merge(RIR,h_measured, h_measured_av);
% Plot and compare!
res.plot_time_dB
%% Nonlinearities - Spectrogram of g(t)
close all, clc
MS2 = itaMSTFdummy;
MS2.outputamplification = 0;
MS2.nonlinearCoefficients = [1 0.1 0.1 0.1];
g = MS2.run_raw;
g.plot_spectrogram
%% Impulse response of non-linear system
h = MS2.run;
h.plot_time_dB
%% Nonlinearities with RIR noise and quantization
MS.outputamplification = 0;
MS.nonlinearCoefficients = [1 0.1 0.1 0.1];
MS.noiselevel = -10;
h = MS.run;
h.plot_time_dB
%% measurement setup
ccx
ita_preferences('useMeasurementChain',0);
ccx
MS = itaMSTF;
MS.inputChannels = 1;
MS.outputChannels = 1;
MS.fftDegree = 17;
MS.freqRange = [10 25000]
%% electrical reference
MS.outputamplification = 0
ita_robocontrol(0,'lineref',0)
MS.init
MS.run_latency;
%%
MS.run_reference
ref = MS.reference;
ref.plot_all
%% run transfer function measurement
ita_robocontrol(0,'norm',0)
MS.outputamplification = -10;
a = MS.run
a.ptd
%%
b = ita_time_window(a,[0.3 0.4],'time')
b.pf
%% signal measurement - calibrated
ccx
ita_preferences('useMeasurementChain',1);
ccx
MS = itaMSRecord;
MS.inputChannels = 3;
%%
MS.calibrate
%% impedance setup
ccx
MS = itaMSImpedance;
MS.fftDegree = 18;
%% calibrate with known resistor
MS.calibrate
%% run impedance measurement of loudspeaker
MS.outputamplification = 20
Z_mit = MS.run
Zs = merge(Z,Z_mit)
Zs.pfp('nodb')
%%
% <ITA-Toolbox>
% This file is part of the application Laboratory for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>
MS = itaMSTF;
%%
MS.inputChannels = 1;
MS.outputChannels = 3;
MS.fftDegree = 17;
%%
close all
MS.fftDegree = 19;
MS.outputamplification = 30;
a = MS.run;
a.plot_time_dB
ita_write(a,['rir_aula_' num2str(idx)],'overwrite')
idx = idx + 1;
%%
rt = ita_roomacoustics(a.merge)
%%
rt(3).bar
%%
L = ita_roomacoustics_EDC(a.merge);
L.plot_time_dB
\ No newline at end of file
%% Tutorial about how to perform a practical measurement
% This function should explain the basic steps to perform measurments with
% the ITA-Toolbox.
%
% To do:
% + namen der Funktion ndern: diese ist wirklich an Anwenden der Messklasse und ita_tutorial_measurement ist mehr zum verstehen der Theorie
% + diese playrec versionen erklren, also wenn man immer nur 2 channel hat oder keine soundkarte. aber besser irgendwo unten in der Datei weil es ja nicht jeden interessiert
% + useMeasurment chain
% + calibrate
% <ITA-Toolbox>
% This file is part of the application Measurement for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>
%% hardware requirements: sound card and AISO driver
% - You need a sound card with ASIO drivers. (???)
% - Alternative: ASIO For all (http://www.asio4all.com/)
%% select your sound card
% call ita_preferences, navigate to 'IO Settings' and select your sound
% card as recording and playing device
ita_preferences
%% measurement of transfer function: quick way
% The class itaMSTF allows the measurement of tranfer functions
MS = itaMSTF
% the measurement setup can be modified via the GUI:
MS.edit
% run measurment
impulseResponse = MS.run
%% define all measurment options in the code
trackLength = 4; % length of excitation signal in seconds
type = 'exp'; % type of excitation signal: exponential sweep
freqRange = [20,12000]; % frequency range of sweep
stopMargin = 0.1; % the last part of the excitation is silent to allow all frequencies to decay
averages = 1; % number of averages
commentStr = 'Example measurement 2014-11-27';
inputChannels = 1:3;
outputChannels = 1;
outputamplification = -35; % Digital output amplification in dBFS. 0 dBFS is maximum. This amplification is automatically compensated in the measurement.
useMeasurementChain = false; % measurement chain is needed for calibration
pauseTime = 0; % time in seconds pause before measurement
% create measurement object with defined parameters
MS = itaMSTF('freqRange', freqRange, 'trackLength', trackLength, 'stopMargin', stopMargin, ...
'inputChannels', inputChannels, 'outputChannels', outputChannels, 'averages', averages, ...
'pause' , pauseTime, 'comment', commentStr, 'type', type, 'outputamplification', outputamplification, ...
'useMeasurementChain', useMeasurementChain);
%% estimate signal to noise ratio
% this method records background noise and raw signal and calculates the
% signal to noise ratio in frequency bands
snr = MS.run_snr;
snr.plot_freq
[snr, signalRec, noiseRec] = MS.run_snr;
ita_plot_freq(mere(signalRec, noiseRec))
%% measurment with calibrated measurement chain
% - define chain with GUI
% - define chain in script
% - 'none' => ignored when calibration, 'unknown' => ???,
% - default sensitivity values are only true if all part of the chain are
% calibrated correctly
% - explain calibration GUI
% - pistonphone => mic, (or data sheet for dummy head or Fig-of-eight mic)
% - voltage calibration => AD & PreAmp,
% - input channel feedback => DA & Amp
%
% -
%% further measurement classes
itaMSTFdummy % transfer measurement class that simulates measurement (including transfer function, quantization, background noise and nonlinearities)
itaMSTFbandpass % transfer function measurement with automatic split of frequency bands to different channels (freqRange has to be matrix)
itaMSTFmimo % supports compensation for more than one output channel
itaMSTFinterleaved % interleaved sweeps to measure multiple output channles in short time
itaMSPlaybackRecord % play back a signal and record input. no deconvolution
itaMSRecord % just record, no ouput
itaMSmls % measure with maximum length sequences (MLS) (incl. Hadamard transformation)
itaMSImpedance % measure impedances (ITA Robo or comparable needed)
cd(fileparts(which(mfilename))); %change the current folder
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
tutorialName = mfilename;
% cut '_compileDocument' from the full name of this m-file
tutorialName =strrep(tutorialName, '_compileDocument_', '_');
web(publish(tutorialName,struct('evalCode',false,'outputDir',pwd)));
cd(fileparts(which(mfilename))); %change the current folder
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
tutorialName = mfilename;
% cut '_compileDocument' from the full name of this m-file
tutorialName =strrep(tutorialName, '_compileDocument_', '_');
web(publish(tutorialName,struct('evalCode',false,'outputDir',pwd)));
function ita_demoOverview(varargin)
%ITA_TUTORIALOVERVIEW - gives an overview of all tutorials
% This function ++++ FILL IN INFO HERE +++
%
% Syntax:
% ita_tutorialOverview
%
%
% Example:
% ita_tutorialOverview()
%
% See also:
% ita_toolbox_gui, ita_read, ita_write, ita_generate
%
% Reference page in Help browser
% <a href="matlab:doc ita_tutorialOverview">doc ita_tutorialOverview</a>
% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>
% Author: Martin Guski -- Email: mgu@akustik.rwth-aachen.de
% Created: 15-Dec-2014
%% search all ita_demo_* files and read description
allTutorialFiles = rdir([ita_toolbox_path filesep '**' filesep 'ita_demo*.m']);
nFiles = numel(allTutorialFiles);
fileNames = cell(nFiles,1);
tutorialDescriptions = cell(nFiles,1);
for iFile = 1:nFiles
[~,fileNames{iFile}] = fileparts(allTutorialFiles(iFile).name);
fid = fopen(allTutorialFiles(iFile).name, 'r');
firstLine = fgetl(fid);
tutorialDescriptions{iFile} = strrep(firstLine, '%% ', '');
fclose(fid);
end
% add mian tutorial in front
idxDemosound = find(strcmp(fileNames, 'ita_demosound'));
idxOverView = find(strcmp(fileNames, 'ita_demoOverview'));
sortOrder = [setdiff(1:nFiles, [idxDemosound idxOverView])];
fileNames = fileNames(sortOrder);
tutorialDescriptions = tutorialDescriptions(sortOrder);
%% display overview with links to tutorials
fprintf('\n\n*** List of tutorials in ITA-Toolbox: %s\n', repmat('*',1,85))
columnWidth = max(cellfun(@length, fileNames));
for iFile = 1:numel(fileNames)
linkText = ['<a href = " matlab: edit ' fileNames{iFile} '">' fileNames{iFile} '</a> ' ];
fprintf('* %s %s : %s \n', linkText, repmat(' ', 1,columnWidth-numel(fileNames{iFile} )), tutorialDescriptions{iFile})
end
fprintf('%s\n', repmat('*',1,125))
%end function
end
\ No newline at end of file
%% Tutorial to illustrate the effect of impulsive noise during sweep measurements
%% Illustration of the effect of impulsive noise during sweep measurements
% and a new automatic impulsive noise detection
% technique.
%
......
%% DEGA Intensivkurs 2012 - PDI
%Init
% <ITA-Toolbox>
% This file is part of the application Laboratory for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>
ccx;
ita_preferences('toolboxlogo',false);
%% Introduction to Discrete Fourier Transform
f = 1000; % Sine frequency in Hz.
amplitude = 1; % Amplitude.
fftDegree = 14; % Signal length, see explanation below.
sr = 44100; % Sampling rate in 1/s.
% Generate the signal
sine = ita_generate('sine',amplitude,f,sr,fftDegree);
sine.nSamples = 890;
sine.plot_time % Plot resulting sine signal in the time domain. Reminder: Take a look at the frequency domain!
%% Audio Signal Processing with Music Signal
% Open a music file. This could also be a measurement or recording.
s = ita_demosound;
s.plot_time
% play back the signal
s.play;
s.plot_freq
%% view the spectrogram
s.plot_spectrogram
%% Influence of the phase;
% Take the music signal and delete the phase information.
close all
g_nophase = ita_zerophase(s);
g_nophase.plot_freq_phase
g_nophase.play
%% Add a random phase but take the original magnitude of the spectrum
g_randphase = ita_randomize_phase( s );
g_randphase.plot_freq_phase;
g_randphase.play;
return %% back to PPT !
%% Generate perfect impulse
close all, clc, fftDegree = 16; sr = 44100;
impulse = ita_generate('impulse',1,sr,fftDegree);
impulse.plot_time('xlim',[-1 2])
impulse.play
%% Deconvolution with Music
s = ita_demosound;
h = fft(s) / fft(s);
h.plot_spkphase
%% Avoid Division by zero
H = ita_divide_spk(s, s, 'regularization',[100 10000]);
H.plot_freq
return %% back to PPT !
%% Measurement Signals -- Generate white noiselevel
close all, clc, fftDegree = 16; sr = 44100;
noise = ita_generate('noise',1,sr,fftDegree);
noise = ita_normalize_dat(noise); % Normalize to 0dBFS
noise.plot_spectrogram
noise.play
%% Generate linear sweep
sweep = ita_generate_sweep('mode','lin','fftDegree',fftDegree, 'samplingRate',sr , 'freqRange', [40 18000]);
sweep.plot_spectrogram
sweep.play
%% Generate exponential sweep
sweep = ita_generate_sweep('fftDegree',fftDegree, 'samplingRate',sr , 'freqRange', [40 18000]);
sweep.plot_spectrogram
sweep.play
return %% back to PPT !
%% Measurement of an LTI system
% some parameters for stereo measurement
inputChannels = 1; outputChannels = 3;
freq_range = [20 20000];
s = ita_generate_sweep('mode','exp','freqRange',freq_range); %,'stopMargin',stopmargin,'fftDegree',fft_degree);
% perform the measurement by simply playing back the signal and recording
% the response
g = ita_portaudio(s,'inputChannels',inputChannels,'outputChannels',outputChannels);
% look at the signal and the (normalized) result in time domain, then frequency domain
s_and_g = ita_merge(s,ita_normalize_dat(g));
s_and_g.plot_time;
%% Spectrogram
g.plot_spectrogram
%% Deconvolution
S = fft(s);
G = fft(g);
H = ita_divide_spk(G, S, 'regularization',freq_range);
H.plot_freq
%% transform to time domain
h = ifft(H);
h.plot_time_dB('xlim',[0 min(0.5,double(s.trackLength))])
return %% back to PPT !
%% Measurement of Impulse Response
MS = itaMSTF;
MS.fftDegree = 18; MS.inputChannels = 1; MS.outputChannels = 3;
MS.latencysamples = 1024;
MS.outputamplification = 20;
h = MS.run
h.plot_time_dB
%% Calculation of Reverberation Time
h.trackLength = 3;
rt = ita_roomacoustics(h, 'freqRange', [200 8000], 'bandsPerOctave', 3, 'T40' );
rt.bar
%% Clarity Index
c80 = ita_roomacoustics(h, 'freqRange', [200 8000], 'bandsPerOctave', 3, 'C80' );
c80.bar
return %% back to PPT !
%% Nonlinearities - Spectrogram of g(t)
close all, clc
MS = itaMSTFdummy;
MS.outputamplification = 0;
MS.nonlinearCoefficients = [1 0.1 0.1 0.1];
g = MS.run_raw;
g.plot_spectrogram
%% Impulse response
h = MS.run;
h.plot_time_dB
return %% back to PPT !
This diff is collapsed.
%% Lambda Resonator Demo Function (see input arguments)
function [ frames ] = ita_demo_lambdaResonator( varargin )
%ita_demo_LAMBDARESONATOR - +++ Short Description here +++
% This function ++++ FILL IN INFO HERE +++
%
% Syntax:
......
%% Akustische Messtechnik exercise
% _2016 JCK, ROP
%
% WARNING: This script contains blanks to be filled
%
%% Block A - Anechoic Chamber
%% A4 - Loudspeaker Transfer Function
% Measurement Setup
MS = itaMSTF;
MS.inputChannels = %%
MS.outputChannels = %%
MS.fftDegree = %%
MS.repeats = %%
% Calibrate