Skip to content

Commit

Permalink
filter-designer added rc hpf and lrc
Browse files Browse the repository at this point in the history
  • Loading branch information
ChSotiriou committed Jan 5, 2025
1 parent 3d15cd8 commit 4b37390
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 1,001 deletions.
120 changes: 89 additions & 31 deletions dist/tools/general/analog-filter-designer/assets/filters.drawio

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/tools/general/analog-filter-designer/assets/lrc_lpf.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 36 additions & 10 deletions dist/tools/general/analog-filter-designer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,63 @@
<div class="col-md-8 offset-md-2">
<h1 class="text-center mb-4">Analog Filter Designer</h1>

<div class="row input-group mb-3">
<div class="input-group mb-3">
<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Filter Type Selection">Type</span>
<select class="form-control input-field" id="select-filter-type">
<option value="se_rc">Single Ended RC</option>
<option value="se_rc">Single Ended RC LPF</option>
<option value="se_rc_hpf">Single Ended RC HPF</option>
<option value="diff_rc">Differential RC</option>
<option value="lrc_lpf">LRC LPF</option>
</select>
</div>

<div class="input-group mb-3 div-type-se_rc div-type-diff_rc type-input-group">
<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Max Simulation Frequency">$f_{max}$</span>
<input required type="text" class="form-control mode-specific input-field type-se_rc type-diff_rc"
<input required type="text" class="form-control mode-specific input-field type-se_rc type-se_rc_hpf type-diff_rc type-lrc_lpf"
id="max-freq" placeholder="(e.g. 100k, 10Meg)" value="1Meg">
</div>

<!-- <div class="input-group mb-3 div-type-se_rc div-type-diff_rc div-type-se_rc_hpf div-type-lrc_lpf type-input-group"> -->
<!-- </div> -->

<div class="input-group mb-3 div-type-se_rc div-type-diff_rc div-type-se_rc_hpf type-input-group">
<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Resistance R1 value">$R_1$</span>
<input required type="text" class="form-control mode-specific input-field type-se_rc type-diff_rc"
<input required type="text" class="form-control mode-specific input-field type-se_rc type-se_rc_hpf type-diff_rc"
id="RC_R1" placeholder="(e.g. 100k, 1Meg)" value="1k">

<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Capacitance C1 value">$C_1$</span>
<input required type="text" class="form-control mode-specific input-field type-se_rc type-diff_rc"
<input required type="text" class="form-control mode-specific input-field type-se_rc type-se_rc_hpf type-diff_rc"
id="RC_C1" placeholder="(100n, 1u)" value="100n">
</div>

<div hidden class="input-group mb-3 div-type-lrc_lpf type-input-group">
<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Inductance L1 value">$L_1$</span>
<input required type="text" class="form-control mode-specific input-field type-lrc_lpf"
id="LRC_L1" placeholder="(100n, 1u)" value="10u">

<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Resistance R1 value">$R_1$</span>
<input required type="text" class="form-control mode-specific input-field type-lrc_lpf"
id="LRC_R1" placeholder="(e.g. 100m)" value="100m">

<span class="input-group-text bg-dark text-white" data-toggle="tooltip"
title="Capacitance C1 value">$C_1$</span>
<input required type="text" class="form-control mode-specific input-field type-lrc_lpf"
id="RC_C1" placeholder="(100n, 1u)" value="10u">
</div>

<hr />

<div class="row row-no-gutters">
<div class="col-xs-12 col-md-4 align-middle text-align-middle fs-2">
<div class="col-xs-12 col-md-4 align-middle text-align-middle fs-4">
<div class="filter-equation" id="eq-se_rc">$$\omega_c = \frac{1}{R_1C_1}$$</div>
<div hidden class="filter-equation" id="eq-se_rc_hpf">$$\omega_c = \frac{1}{R_1C_1}$$</div>
<div hidden class="filter-equation" id="eq-diff_rc">$$\omega_c = \frac{1}{2R_1C_1}$$</div>
<div hidden class="filter-equation" id="eq-lrc_lpf">
$$\omega_r = \frac{1}{\sqrt{L_1C_1}}$$
</div>
</div>
<div class="col-xs-6 col-md-8">
<img style="width: 100%;" src="assets/se_rc.svg" id="filter-img"></img>
Expand Down Expand Up @@ -154,11 +179,12 @@ <h1 class="text-center mb-4">Analog Filter Designer</h1>
<script src="https://cdn.plot.ly/plotly-2.35.2.min.js" charset="utf-8"></script>

<!-- External JavaScript (now in the /js folder) -->
<script src="js/complex.min.js"></script>
<script src="js/ngrp.js"></script>
<script src="js/ngspice.js"></script>
<script src="js/models.js"></script>
<script src="js/main.js"></script>

</body>

</html>
</html>
26 changes: 26 additions & 0 deletions dist/tools/general/analog-filter-designer/js/complex.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 32 additions & 9 deletions dist/tools/general/analog-filter-designer/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,53 @@ function processSimulation() {
rawData = raw
data = ngrp(raw)

freq = data['data'][0]['v1']
freq = data['data'][0]['value'].map(x => x.abs())

vout_p = data['data'].filter(x => x.name.toLowerCase() == 'v(vout+)')[0]
vout_n = data['data'].filter(x => x.name.toLowerCase() == 'v(vout-)')[0]

y_data = vout_p['v1']
if (vout_n != undefined) y_data = y_data.map((x, i) => x - vout_n['v1'][i])
vout = vout_p['value']
if (vout_n != undefined) vout = vout.map((x, i) => x.sub(vout_n['value'][i]))

y_data = y_data.map(x => 10*Math.log10(x))
cutoff = freq[y_data.indexOf(y_data.reduce((c, v) => Math.abs(v - (-3)) < Math.abs(c - (-3)) ? v : c ))]
amplitude = vout.map(x => 10*Math.log10(x.abs()))
phase = vout.map(x => 180 * x.arg() / Math.PI)
cutoff = freq[amplitude.indexOf(amplitude.reduce((c, v) => Math.abs(v - (-3)) < Math.abs(c - (-3)) ? v : c ))]

Plotly.newPlot(document.getElementById('plot'), [{
x: freq,
y: y_data
y: amplitude
}, {
x: freq,
y: phase,
yaxis: 'y2',
line: {
dash: 'dot',
}
}], {
margin: { t: 0 },
showlegend: false,
yaxis: {
title: "Attenuation (dB)",
title: {
text: "Attenuation (dB)",
font: {color: '#1F77B4'}
},
tickfont: {color: '#1F77B4'},
},
yaxis2: {
title: {
text: 'Phase (deg)',
font: {color: '#FF7F0E'}
},
tickfont: {color: '#FF7F0E'},
overlaying: 'y',
side: 'right'
},
xaxis: {
type: 'log',
title: "Frequency (Hz)",
},
shapes: [
plottly_xline(cutoff, y_min=Math.min(...y_data), y_max=0)
plottly_xline(cutoff, y_min=Math.min(...amplitude), y_max=Math.max(...amplitude))
]
}, {
responsive: true
Expand Down Expand Up @@ -92,4 +115,4 @@ function update() {
Array.from(document.getElementsByClassName('input-field')).forEach(x => x.addEventListener('change', update))
setTimeout(() => update(), 1000)

document.getElementById('downloadRawData').addEventListener('click', x => downloadBlob(rawData, 'out.raw'))
document.getElementById('downloadRawData').addEventListener('click', x => downloadBlob(rawData, 'out.raw'))
30 changes: 27 additions & 3 deletions dist/tools/general/analog-filter-designer/js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ downloadURL = function(data, fileName) {
};


const sim_command = `.ac dec 100 1 {0}`
const sim_command = `.ac dec 500 1 {0}`

const sim_models = {
'se_rc': `Single Ended RC
// ----------------------------------------
'se_rc': `Single Ended RC LPF
R1 Vin Vout+ {1}
C1 Vout+ 0 {2}
VCC Vin 0 AC 1.
Expand All @@ -41,6 +42,18 @@ ${sim_command}
.end`,

// ----------------------------------------
'se_rc_hpf': `Single Ended RC HPF
C1 Vin Vout+ {2}
R1 Vout+ 0 {1}
VCC Vin 0 AC 1.
${sim_command}
.save V(vout+)
.end`,

// ----------------------------------------
'diff_rc': `Differential RC
R1 Vin Vout+ {1}
Expand All @@ -54,8 +67,19 @@ ${sim_command}
.end`,

// ----------------------------------------
'lrc_lpf': `LRC LPF 1
L1 Vin 1 {1}
R1 1 Vout+ {2}
C1 Vout+ 0 {3}
VCC Vin 0 AC 1.
${sim_command}
.save V(vout+)
.end`,
}

const spiceinit = `
set filetype=ascii
`
`
12 changes: 5 additions & 7 deletions dist/tools/general/analog-filter-designer/js/ngrp.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ function ngrp(data) {
out['data'].push({
'name': varName,
'type': type,
'v1': [],
'v2': [],
'value': [],
})
}

Expand All @@ -60,12 +59,11 @@ function ngrp(data) {
for (let j = 0; j < out['varCount']; j++) {
values = next().split("\t").slice(-1)[0].split(",")

v1 = parseFloat(values[0])
v2 = parseFloat(values[1])
out['data'][j]['v1'].push(v1)
out['data'][j]['v2'].push(v2)
re = parseFloat(values[0])
im = parseFloat(values[1])
out['data'][j]['value'].push(Complex(re, im))
}
}

return out
}
}
Loading

0 comments on commit 4b37390

Please sign in to comment.