ita_demo_intensivkurs_messtechnik_dega.m 10.8 KB
Newer Older
Johannes Klein's avatar
Johannes Klein committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
%% Intensivkurs Raumakustik Messtechnik (DEGA version)
% Combined lecture and excercise
%
% WARNING: This excersise might require measurements conducted outside 
% this demo script.
%
% <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>
%
% _2016 - JCK, GKB
% toolbox-dev@akustik.rwth-aachen.de

warning('off','MATLAB:nargchk:deprecated');

%% Outputsettings
double_outputamp = -6;
double_latencysamples = 8605;
double_inputChannel = 1;
double_outputChannel = 1;

%% SLIDES - ISO 3382, Messung von bertragunsstrecken, LTI-Systeme, Quantisierung, Abstastung, ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Load demo sound for further demos
sound = ita_demosound; % Built-in toolbox demo sound

%% Signals Time and Frequency

% Generate 200Hz sine
sine = ita_generate('sine',1,200,44100,16, 'fullperiod');

% Take a look
sine.pt
xlim([0 0.11]);
xlim([1.44 1.55]);

% Fourier Transformation
% Absolute value and phase
sine.pfp; clc;

% Commonly displayed without phase
sine.pf

% Fourier Transformation expects periodic signals
% Crop sine so it's not a full period anymore
sine_crop = ita_time_crop(sine, [1 sine.nSamples-163]);
sine_crop.pt
xlim([1.44 1.55]);

% Take a look
sine_crop.pf

% What is the influence of the phase?
% Take a look a music signal
sound_amp = ita_amplify_to(sound, double_outputamp);
sound_amp.play;
sound.pt
sound.pfp

% Erase Phase -> Music becomes impulse
test = sound;
test.freqData = abs(sound.freqData) .* exp(1i*0);
test.pfp
test.pt
xlim([-0.2 0.5]);

%% Quantization

% Play with 16bit
sound_amp = ita_amplify_to(sound, double_outputamp);
sound_amp.play;

% Re-quantize the signal and listen to the effect
% 8bit -> 256
quant_8bit = ita_quantize(sound,'bits',8);
quant_8bit_amp = ita_amplify_to(quant_8bit, double_outputamp);
quant_8bit_amp.play;

% 4bit -> 16 values
quant_4bit = ita_quantize(sound,'bits',4);
quant_4bit_amp = ita_amplify_to(quant_4bit, double_outputamp);
quant_4bit_amp.play;
quant_4bit.pt

% 2bit -> 4 values
quant_2bit = ita_quantize(sound,'bits',2);
quant_2bit_amp = ita_amplify_to(quant_2bit, double_outputamp);
quant_2bit_amp.play;
quant_2bit.pt

%% Downsampling

% Play with std. samplingrate
sound.samplingRate

% Downsampling without lowpass
% Take every 20th sample -> 20 times lower sampling rate
sound1 = sound;
sound1.timeData = sound1.timeData(1:20:end);
sound1.samplingRate = sound1.samplingRate / 20;

comp = merge(sound,sound1);
comp.pf
xlim([20 900]);

% Downsampling with lowpass
sound2 = sound;

sound2 = ita_filter_bandpass(sound2,'upper',sound2.samplingRate/2/20);

sound2.timeData = sound2.timeData(1:20:end);
sound2.samplingRate = sound2.samplingRate / 20;

comp = merge(sound,sound1, sound2);
comp.pf
xlim([20 900]);

%% Upsampling

% Generate perfect impulse at 192kHz
sound3 = ita_generate('emptydat',192000,16);
sound3.channelNames = {'Test Signal'};
sound3.timeData = zeros(2^16,1);
sound3.timeData(192000*0.15+2,1) = 1;
sound3.pt; xlim([0.1498 0.1502]); ylim([-0.5 1]);
% As seen before, only band-limited signals can be represented in digital
% systems. At 192kHz upper frequency is 96kHz. To make sure we take 44kHz.
sound3_44 = ita_filter_bandpass(sound3,'upper',44000, 'order', 40);
sound3_44.pt; xlim([0.1498 0.1502]); ylim([-0.5 1]);
 
% We would like to downsample it to 48kHz though. Therefore the upper 
% frequency is 24kHz. To make sure we limit to 18kHz.
sound3_18 = ita_filter_bandpass(sound3,'upper',18000, 'order', 40);
sound3_18.pf
sound3_18.pt; xlim([0.1498 0.1502]); ylim([-0.5 1]);

% Downsampled to 48kHz, the peak is not at the same time.
sound3_18_down = ita_resample(sound3_18, 48000);
sound3_18_down.pt; xlim([0.1498 0.1502]); ylim([-0.5 1]);

% However, upsampling the bandlimited signal reconstructs the original
sound3_18_down_up = ita_resample(sound3_18_down, 192000);
sound3_18_down_up.pt; xlim([0.1498 0.1502]); ylim([-0.5 1]);

% Contrary to the signal containing frequencies up to 44Khz.
sound3_44_down = ita_resample(sound3_44, 48000);
sound3_44_down_up = ita_resample(sound3_44_down, 192000);
sound3_44_down_up.pt; xlim([0.1498 0.1502]); ylim([-0.5 1]);


%% SLIDES - Anregungssignale, Mess-Signale, Moderne Methoden, Enfaltung, MLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% MLS

% Generate MLS
MLS = ita_generate_mls('fftDegree', 20);

% Take a look
MLS.pt; xlim([1 1.01]);

% Check auto-correlation
auto_correlation = ita_xcorr_dat(MLS,MLS);
auto_correlation.pt, xlim([0 0.0005]);

% Do a measurement with it
MS = itaMSmls;
MS.inputChannels = double_inputChannel;
MS.outputChannels = double_outputChannel;
MS.fftDegree = 20;
MS.stopMargin = 1;
MS.latencysamples = double_latencysamples;
MS.outputamplification = 20;

result = MS.run;
result.ptd

%% SLIDES - Spektrale Division 2-Kanal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Generate random signal, aka noise
measurement_noise = ita_generate('noise',1,44100,18);
measurement_noise = ita_mpb_filter(measurement_noise, [200 4000]);
measurement_noise.pf

% Generate transfer function without phase
tf = ita_generate('flat',1,44100,18);
tf = ita_mpb_filter(tf, [20 10000]);
tf.pt; xlim([0 0.04]);
tf.pf

% Convolution without additive noise
result = measurement_noise * tf;
result.signalType = 'energy';
result.pt
result.pf

% Deconvolution
tf_recovered_clean = result / measurement_noise;
tf_recovered_clean.pt; xlim([0 0.04]);
tf_recovered_clean.pf
tf_compare = merge(tf, tf_recovered_clean);
tf_compare.pf

% What happens in noisy system?
additive_noise = ita_generate('noise',0.1,44100,18);

% Convolution with additive noise
result = measurement_noise * tf;
% Add noise in time domain
result = result + additive_noise;
result.signalType = 'energy';
result.pt
result.pf

% Deconvolution
tf_recovered_noise = result / measurement_noise;
tf_recovered_noise.pt; xlim([0 0.04]);
tf_recovered_noise.pf
% What's the effect in the time domain?
comp = merge(tf_recovered_clean, tf_recovered_noise);
comp.pf
comp.pt

% Regularization

tf_recovered_reg = result * ita_invert_spk_regularization(measurement_noise, [200 4000]);
tf_recovered_reg.pt; xlim([0 0.04]);
tf_recovered_reg.pf

comp = merge(tf_recovered_clean, tf_recovered_noise, tf_recovered_reg);
comp.pf;

% Real example measurement
MS = itaMSTF;
MS.inputChannels = double_inputChannel;
MS.outputChannels = double_outputChannel;

MS.fftDegree = 20;
MS.stopMargin = 1;
MS.latencysamples = double_latencysamples;

% Example measurement with noise
MS.type = 'noise';

MS.outputamplification = 3;
result = MS.run;
result.ptd
result.pf

% Example measurement with music
MS.excitation = ita_demosound;

MS.outputamplification = 3;
result = MS.run;
result.ptd
result.pf

%% SLIDES - Spektrale Division - Sweep
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Real example measurement
MS = itaMSTF;
MS.inputChannels = double_inputChannel;
MS.outputChannels = double_outputChannel;

MS.fftDegree = 18;
MS.stopMargin = 1;
MS.latencysamples = double_latencysamples;

% Take a look at the sweep
MS.excitation.pt
xlim([0 5]);
MS.excitation.pf
MS.excitation.plot_spectrogram

% Let's do this
MS.outputamplification = 10;
impulse_response_1 = MS.run;
impulse_response_1.ptd;

% How do I get a better SNR?
% Averaging
MS.averages = 2;
impulse_response_2 = MS.run;
impulse_response_2.ptd;

% MS.averages = 4;
% impulse_response_4 = MS.run;
% impulse_response_4.ptd;

% comp = merge(impulse_response_1, impulse_response_2, impulse_response_4);
comp = merge(impulse_response_1, impulse_response_2);
comp.pf;

% Longer measurement Signal
MS.fftDegree = 19;
impulse_response_19 = MS.run;
impulse_response_19.ptd;

MS.fftDegree = 20;
impulse_response_20 = MS.run;
impulse_response_20.ptd;

comp = merge(impulse_response_19, impulse_response_20);
comp.pf;

% Non-linearities
MS = itaMSTFdummy;
MS.inputChannels = double_inputChannel;
MS.outputChannels = double_outputChannel;

MS.fftDegree = 20;
MS.stopMargin = 1;
MS.freqRange = [20 20000];
MS.averages = 1;
MS.outputamplification = 0;
MS.systemresponse = ita_time_shift(ita_mpb_filter(ita_generate('impulse',1,MS.samplingRate,16), [20 20000]), 0.5, 'time');

MS.nonlinearCoefficients = [1 1 1 1];
MS.noiselevel            = -100;

result = MS.run_raw;
result.plot_spectrogram;

impulse_response = MS.run;
impulse_response.ptd

% Window non-linearities?
impulse_response_win = ita_time_window(impulse_response, [0.1 0 1 1.1], 'time');
comp = merge(impulse_response, impulse_response_win);
comp.ptd
comp.pf

% Is that the correct result? Nope! Level off-set
MS.nonlinearCoefficients = 1;
impulse_response_clean = MS.run;
impulse_response_clean_win = ita_time_window(impulse_response_clean, [0.1 0 1 1.1], 'time');
comp = merge(impulse_response_clean_win, impulse_response_win);
comp.pf
comp.ptd
% Where does this come from? It's an effect of the uneven harmonics!
MS.nonlinearCoefficients = [1 1 0 1];
impulse_response_even = MS.run;
impulse_response_even_win = ita_time_window(impulse_response_even, [0.1 0 1 1.1], 'time');
comp = merge(impulse_response_clean_win, impulse_response_win, impulse_response_even_win);
comp.pf
comp.ptd

% How are they created? Everything that is hard limiting! E.g. digital
% clipping
sine = ita_generate('sine',1,200,44100,20);
sine.timeData(sine.timeData > 0.7) = 0.7;
sine.timeData(sine.timeData < -0.7) = -0.7;
sine.pt; xlim([2.0 2.3]); ylim([0.65 0.73]);
sine.pf

%% SLIDES - Impulsantwort und Nachhallzeit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Measured earlier with a long sweep
impulse_response_20.ptd

% Possible to get Energy Decay Curve (EDC, Schrder Plot)?

ra = ita_roomacoustics(impulse_response_20, 'EDC', 'T20', 'broadbandAnalysis');
ra.EDC.ptd

ra.T20.freqData