# 分析谐波失真

### 查看非线性的影响

```VmaxPk = 2; % Maximum operating voltage Fi = 2000; % Sinusoidal frequency of 2 kHz Fs = 44.1e3; % Sample rate of 44.1kHz Tstop = 50e-3; % Duration of sinusoid t = 0:1/Fs:Tstop; % Input time vector % Use the maximum allowable voltage of the amplifier inputVmax = VmaxPk*sin(2*pi*Fi*t); outputVmax = helperHarmonicDistortionAmplifier(inputVmax);```

```plot(t, outputVmax) xlabel('Time') ylabel('Output Voltage') axis([0 5e-3 -2.5 2.5]) title('Amplifier output')```

`helperPlotPeriodogram(outputVmax, Fs, 'power','annotate');`

### 量化非线性失真

`thd(outputVmax, Fs)`

```ans = -60.3888 ```

`snr(outputVmax, Fs)`

```ans = 130.9300 ```

`sinad(outputVmax, Fs)`

```ans = 60.3888 ```

THD、SNR 和 SINAD 分别为 -60 dB、131 dB 和 60 dB。由于 THD 的幅值与 SINAD 大致相等，我们可以推断大部分失真是由谐波失真引起的。

### 降低谐波失真的输入衰减

```inputVhalf = (VmaxPk/2) * sin(2*pi*Fi*t); outputVhalf = helperHarmonicDistortionAmplifier(inputVhalf); helperPlotPeriodogram(outputVhalf, Fs, 'power','annotate');```

`thdVhalf = thd(outputVhalf, Fs)`
```thdVhalf = -72.0676 ```
`snrVhalf = snr(outputVhalf, Fs)`
```snrVhalf = 124.8767 ```
`sinadVhalf = sinad(outputVhalf, Fs)`
```sinadVhalf = 72.0676 ```

### SNR THD 和 SINAD 当作输入衰减的函数

```% Allocate a table with 30 entries nReadings = 30; distortionTable = zeros(nReadings, 3); % Compute the THD, SNR and SINAD for each of the attenuation settings for i = 1:nReadings inputVbestAtten = db2mag(-i) * VmaxPk * sin(2*pi*Fi*t); outputVbestAtten = helperHarmonicDistortionAmplifier(inputVbestAtten); distortionTable(i,:) = [abs(thd(outputVbestAtten, Fs)) snr(outputVbestAtten, Fs) sinad(outputVbestAtten, Fs)]; end % Plot results plot(distortionTable) xlabel('Input Attenuation (dB)') ylabel('Dynamic Range (dB)') legend('|THD|','SNR','SINAD','Location','best') title('Distortion Metrics vs. Input Attenuation')```

```% Search the table for the largest SINAD reading [maxSINAD, iAtten] = max(distortionTable(:,3)); fprintf('Max SINAD (%.1f dB) occurs at %.f dB attenuation\n', ... maxSINAD, iAtten)```
```Max SINAD (103.7 dB) occurs at 26 dB attenuation ```

```inputVbestAtten = db2mag(-iAtten) * VmaxPk * sin(2*pi*Fi*t); outputVbestAtten = helperHarmonicDistortionAmplifier(inputVbestAtten); helperPlotPeriodogram(outputVbestAtten, Fs, 'power','annotate','shownoise');```

### 用于消除失真的后处理

```inputRamp = -2:0.00001:2; outputRamp = helperHarmonicDistortionAmplifier(inputRamp); polyCoeff = polyfit(outputRamp,inputRamp,3)```
```polyCoeff = 1×4 0.0010 -0.0002 1.0000 -0.0250 ```

```correctedOutputVmax = polyval(polyCoeff, outputVmax); helperPlotPeriodogram([outputVmax; correctedOutputVmax],Fs,'power'); subplot(2,1,1) title('Uncorrected') subplot(2,1,2) title('Polynomial Corrected')```

`thdCorrectedVmax = thd(correctedOutputVmax, Fs)`
```thdCorrectedVmax = -99.6194 ```
`snrCorrectedVmax = snr(correctedOutputVmax, Fs)`
```snrCorrectedVmax = 130.7491 ```
`sinadCorrectedVmax = sinad(correctedOutputVmax, Fs)`
```sinadCorrectedVmax = 99.6162 ```

### 组合方法

```subplot(1,1,1) % Add three more columns to our distortion table distortionTable = [distortionTable zeros(nReadings,3)]; for i = 1:nReadings inputVreduced = db2mag(-i) * VmaxPk * sin(2*pi*Fi*t); outputVreduced = helperHarmonicDistortionAmplifier(inputVreduced); correctedOutput = polyval(polyCoeff, outputVreduced); distortionTable(i,4:6) = [abs(thd(correctedOutput, Fs)) snr(correctedOutput, Fs) sinad(correctedOutput, Fs)]; end h = plot(distortionTable)```
```h = 6x1 Line array: Line Line Line Line Line Line ```
```xlabel('Input attenuation (dB)') ylabel('Dynamic Range (dB)') for i = 1:3 h(i+3).Color = h(i).Color; h(i+3).LineStyle = '--' ; end legend('|THD| (uncorrected)','SNR (uncorrected)','SINAD (uncorrected)', ... '|THD| (corrected)','SNR (corrected)','SINAD (corrected)','Location','best') title('Distortion Metrics vs. Input Attenuation and Polynomial Correction');```

```[maxSINADcorrected, iAttenCorr] = max(distortionTable(:,6)); fprintf('Corrected: Max SINAD (%.1f dB) at %.f dB attenuation\n', ... maxSINADcorrected, iAttenCorr)```
```Corrected: Max SINAD (109.7 dB) at 17 dB attenuation ```

```% Recompute amplifier at maximum SINAD attenuation setting with polynomial inputVreduced = db2mag(-iAttenCorr) * VmaxPk * sin(2*pi*Fi*t); outputVreduced = helperHarmonicDistortionAmplifier(inputVreduced); correctedOutputVbestAtten = polyval(polyCoeff, outputVreduced); helperPlotPeriodogram(correctedOutputVbestAtten, Fs, 'power','annotate','shownoise'); title('Periodogram of attenuated and polynomial corrected amplifier')```