# m01_audio_isolation_transformer_demo_script

Script to check the maths.

## Introduction

If you have two pieces of earth-connected audio equipment, you can get ground-loop hum. It's not a good idea to break safety grounds at the plug, and instead, best practice is to use an audio isolation transformer.

Audio isolation transformers are pretty expensive, which leads to two questions: one is "can I build my own?", and the other is "should I really expect this piece of kit to do what I want". One way to handle this is to make a quick model, which you can do in Simscape.

Audio isolation transformer data sheets are generally not particularly rich, often just giving you an impedance rating and a frequency range, but not a lot else - you won't often get a nice equivalent circuit network model. With a bit of first-year EE maths, you can calculate something that gives you an idea of the ideal behaviours, though it comes with the caveat that it's just the electronic version of a "back of the envelope" calculation. However, this might be all you need - if you're trying to solve a similar problem, why not give it a try?

## Define the transformer parameters

Here, I've assumed a typical data sheet parameter set.

```fMin_Hz = 300; % this is a telephone xformer - try also 20
fMax_Hz = 3e3; % this is a telephone xformer - try also 20e3
Z_Ohm = 10e3;
```

## Calculate the inductances

It's beyond the scope of this document to explain how I calculated the inductances - it's essentially first-year EE stuff. Feel free to have a look at the function findXfmEcnInductances to see how I did it.

```% Display the schematic
bdclose all
copyfile('m02_linearizationModel.slx', 'm02_linMdl_view.slx', 'f');
open_system('m02_linMdl_view.slx');

help findXfmEcnInductances
[Lseries_H, Lshunt_H] = findXfmEcnInductances(Z_Ohm, fMin_Hz, fMax_Hz);
```
``` findXfmEcnInductances Find lossless audio transformer inductances.
[L_series, L_shunt] = findXfmInductances(R_z, fMin_Hz, fMax_Hz)
attempts to find series and shunt inductances for an equivalent circuit
network model of a audio transformer with no resistive losses and a
matched impedance of R_z ohms. The inductances are chosen so that the
half-nominal-power bandwidth runs from fMin_Hz to fMax_Hz. Note that
transformers are parameterized assuming they are scaled for maximum
power transfer between a source and load - these values are compared to
the maximum theoretical power transfer of 50%, and when fMin_Hz and
fMax_Hz are relatively close together, the theoertical maximum is not
achieved between the two frequency limits.

```

## Form a simple transfer function realization

We'll check our EE maths in two steps: first, we'll plot our theoretical transfer function so we can see whether it has the right -3dB points.

```bdclose all                     % cleanup
delete('m02_linMdl_view.slx');

Om_lo = Z_Ohm/(2*Lshunt_H);
Om_hi = 2*Z_Ohm/Lseries_H;

num = [0.5 0];
den = [1/Om_hi (1+2*Om_lo/Om_hi) Om_lo];
G1 = tf(num, den);

clear Om_lo Om_hi num den

figure(1)
clf
legend('theoretical model', '-3 dB line');
grid on
```

## Compare our frequency repsonse to one from a Simscape model

Secondly, we'll linearize the Simscape model and compare it. This will confirm that the maths we've used gives the same results as the schematic.

```load_system('m02_linearizationModel');
G2 = linearize('m02_linearizationModel');

figure(1)
clf
bode(G1, 'b-', G2, 'm--', ss(0.5/sqrt(2)), 'r-.', om_rad)
legend('theoretical model', 'linearized Simscape', '-3 dB line')
grid on
```

## Simulate with different frequencies

Now we're finished, we can look at the circuit behaviour using another model, this time one that lets us simulate the impact of the transformer on sine waves of different frequencies.

Note that the new model uses a separate parameter file - you'll need to change your values in both places if you want to make consistent changes.

```bdclose all
close all
clear

type transformerParameters
open_system('m03_audio_isolation_xformer');
sim('m03_audio_isolation_xformer');
```
```% transformerParameters

% Data sheet transformer parameters
%
% Typical data sheets give us an impedance range and frequency limits.
% That's not a lot to go on! We'll do our best here, using "standard"
% parameters to estimate the series and shunt inductances that match the
% specified frequency limits. The EE math is straighforward but fiddly so
% it's captured in a function.
%
fMin_Hz = 300; % this is a telephone xformer - try also 20
fMax_Hz = 3e3; % this is a telephone xformer - try also 20e3
Z_Ohm = 10e3;

[Lseries_H, Lshunt_H] = findXfmEcnInductances(Z_Ohm, fMin_Hz, fMax_Hz);

% % Configure for power-matched simulation
% %
% % This would be relevant for speaker matching problems.
% %
%
% Rsrc_Ohm = Z_Ohm;