summaryrefslogtreecommitdiff
path: root/macros/helperHarmonicDistortion.sci
blob: 35378692c14058277bb53ff67f14292d1f996d7a (plain)
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
function outputVoltage = helperHarmonicDistortion(inputVoltage)
//helperHarmonicDistortionADC Helper function for HarmonicDistortionExample.m

//Calling Sequence
// outputVoltage=helperHarmonicDistortionAmplifier(inputVoltage)

//Description
//Analizing the harmonic distortion of a weakly non-linear system in the presence of noise.

//Example
//VmaxPk = 2;
//Fi = 2000;
//Fs = 44.1e3;
//Tstop = 50e-3;
//t = 0:1/Fs:Tstop;
//inputVmax = VmaxPk*sin(2*%pi*Fi*t);z
//outputVmax = helperHarmonicDistortionAmplifier(inputVmax);
//plot(t, outputVmax);replot([0,-2.5,0.005,2.5]);
//xlabel('Time')
//ylabel('Output Voltage')
//title('Amplifier output')

// model parameters
noiseVrms = 0.4e-6;  // RMS voltage of input noisefloor

// polynomial coefficients
a0 = 25e-3;  // dc bias (25mV)
a1 = 1;      // voltage gain
a2 = 6e-5;   // second order term
a3 = -1e-3;  // third order term
a4 = 5e-6;   // fourth order term
a5 = 1e-5;   // fifth order term

// polyval function has constant term at the end.
polyCoeff = [a5 a4 a3 a2 a1 a0];

// get number of input samples
n = size(inputVoltage,2);

// add noise at input
inputNoise = noiseVrms*rand(1,n,"normal");
distortedInput = inputVoltage + inputNoise;

// adjust input by DC bias, voltage gain and higher order terms
outputVoltage = polyval(polyCoeff, distortedInput);
endfunction