{ "cells": [ { "cell_type": "markdown", "id": "eb5e3afc", "metadata": {}, "source": [ "# Read Export Files" ] }, { "cell_type": "code", "execution_count": 1, "id": "a1f59e48", "metadata": {}, "outputs": [], "source": [ "# Imports\n", "import numpy as np\n", "import pandas as pd\n", "from sympy.utilities.lambdify import lambdify\n", "import matplotlib.pyplot as plt\n", "import matplotlib\n", "%matplotlib inline\n", "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"all\"\n", "from matplotlib.ticker import LogLocator" ] }, { "cell_type": "code", "execution_count": 2, "id": "4f1dd722", "metadata": {}, "outputs": [], "source": [ "# Official package: https://pypi.org/project/ltspice/\n", "# Derived LTspice reader\n", "def read_ltspice(file_name,ftype='trans',units='db'):\n", " cols = []\n", " arrs = []\n", " with open(file_name, 'r',encoding='utf-8') as data:\n", " for i,line in enumerate(data):\n", " if i==0:\n", " cols = line.split()\n", " arrs = [[] for _ in cols]\n", " continue\n", " parts = line.split()\n", " for j,part in enumerate(parts):\n", " arrs[j].append(part)\n", " df = pd.DataFrame(arrs)\n", " df = df.T\n", " df.astype('float64',errors='ignore')\n", " df.columns = cols\n", " if ftype=='trans':\n", " return df\n", " elif ftype=='ac':\n", " if units=='db':\n", " for col in cols:\n", " if df[col].str.contains(',').all():\n", " df[f'Mag_{col}'] = df[col].apply(lambda x: x.split(',')[0])\n", " df[f'Mag_{col}'] = df[f'Mag_{col}'].apply(lambda x: x[1:-2])\n", " df[f'Mag_{col}'] = df[f'Mag_{col}'].astype('float64')\n", " df[f'Phase_{col}'] = df[col].apply(lambda x: x.split(',')[1])\n", " df[f'Phase_{col}'] = df[f'Phase_{col}'].apply(lambda x: x[0:-2])\n", " df[f'Phase_{col}'] = df[f'Phase_{col}'].astype('float64')\n", " if units=='cartesian':\n", " for col in cols:\n", " if df[col].str.contains(',').all():\n", " df[f'Re_{col}'] = df[col].apply(lambda x: x.split(',')[0])\n", " df[f'Re_{col}'] = df[f'Re_{col}'].astype('float64')\n", " df[f'Im_{col}'] = df[col].apply(lambda x: x.split(',')[1])\n", " df[f'Im_{col}'] = df[f'Im_{col}'].astype('float64')\n", " df['Freq.'] = df['Freq.'].astype('float64')\n", " return df\n", " else:\n", " print('invalid ftype')" ] }, { "cell_type": "code", "execution_count": 6, "id": "be53ec60", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " | Freq. | \n", "V(vout3)/(V(vp)-V(vm)) | \n", "Mag_V(vout3)/(V(vp)-V(vm)) | \n", "Phase_V(vout3)/(V(vp)-V(vm)) | \n", "
---|---|---|---|---|
0 | \n", "0.010 | \n", "(-4.11838015619366e+001dB,-5.59733635143613e-001°) | \n", "-41.184 | \n", "-0.560 | \n", "
1 | \n", "0.010 | \n", "(-4.07837855425980e+001dB,-5.72769990801834e-001°) | \n", "-40.784 | \n", "-0.573 | \n", "