write_spice_file.m
Contents
This function generates a SPICE .cir file. To trace the output of all the filters in SPICE, trace something like VDB(51) + VDB(52) + ... + VDB(510)+.... The w0 and n inputs are also needed. The function doesn't return anything.
function write_spice_file(r1,r2,r5,c3,c4,w0,n)
Header
These lines are responsible for creating a SPICE voltage source, and definining a subcircuit which will be used for all of the OP AMPS in the equalizer. The SPICE file is written into the directory defined by MatLab's cd.
file_1 = fopen(strcat(cd,'/EQ_gen_spice_output.cir'),'wt'); fprintf(file_1,'EQgenerator SPICE OUTPUT\n'); fprintf(file_1,'VS 1 0 AC 1 \n'); fprintf(file_1,'.SUBCKT OPAMP 1 2 3\n'); fprintf(file_1,'RI 1 2 100MEG\n'); fprintf(file_1,'EA 3 0 1 2 100MEG\n'); fprintf(file_1,'.ENDS OPAMP\n');
Creating the Equalizer in SPICE
For each filter, the appropriate SPICE lines are written. Circuit element names are a combination of their name in terms of the multiple feedback filter topology and the current filter being written. For example, R12 is R1 in the second filter of equalizer. Node names are created in a similar fashion.
for filter_n = 1:n %write spice comment indicating which filter we're on s = sprintf('*Center Frequency: f0 = %d\n', w0(filter_n)/(2*pi)); fprintf(file_1,s); index_n = num2str(filter_n); %write the r1 line of the current filter r1_value = sprintf(' %d ',r1(filter_n)); node_1 = sprintf(' %d ',1); node_2 = strcat('2',num2str(index_n)); node_2 = sprintf(' %d ', str2double(node_2)); s = strcat('R1',index_n,node_1,node_2,r1_value,'\n'); fprintf(file_1,s); %write the r2 line of the current filter r2_value = sprintf(' %d ',r2(filter_n)); node_1 = strcat('2',num2str(index_n)); node_1 = sprintf(' %d ', str2double(node_1)); node_2 = sprintf(' 0 '); s = strcat('R2',index_n,node_1,node_2,r2_value,'\n'); fprintf(file_1,s); %write the c3 line of the current filter c3_value = sprintf(' %g ',c3(filter_n)); node_1 = strcat('2',num2str(index_n)); node_1 = sprintf(' %d ', str2double(node_1)); node_2 = strcat('3',num2str(index_n)); node_2 = sprintf(' %d ', str2double(node_2)); s = strcat('C3',index_n,node_1,node_2,c3_value,'\n'); fprintf(file_1,s); %write the c4 line of the current filter c4_value = sprintf(' %g ',c4(filter_n)); node_1 = strcat('2',num2str(index_n)); node_1 = sprintf(' %d ', str2double(node_1)); node_2 = strcat('5',num2str(index_n)); node_2 = sprintf(' %d ', str2double(node_2)); s = strcat('C4',index_n,node_1,node_2,c4_value,'\n'); fprintf(file_1,s); %write the r5 line of the current filter r5_value = sprintf(' %g ',r5(filter_n)); node_1 = strcat('3',num2str(index_n)); node_1 = sprintf(' %d ', str2double(node_1)); node_2 = strcat('5',num2str(index_n)); node_2 = sprintf(' %d ', str2double(node_2)); s = strcat('R5',index_n,node_1,node_2,r5_value,'\n'); fprintf(file_1,s); %write the subcircuit line x_value = sprintf(' OPAMP '); node_1 = strcat('3',num2str(index_n)); node_1 = sprintf(' %d ', str2double(node_1)); node_2 = strcat('5',num2str(index_n)); node_2 = sprintf(' %d ', str2double(node_2)); node_0 = sprintf(' %d ',0); s = strcat('X',index_n,node_0,node_1,node_2,x_value,'\n'); fprintf(file_1,s); end
Footer
After the circuit has been completed, the following lines prepare the AC analysis and finish up the file.
low_freq = sprintf(' %g ',(w0(1)/(2*pi))*.01); high_freq = sprintf(' %g ',(w0(n)/(2*pi))*100); s = strcat('.AC DEC 50',low_freq,high_freq,'\n'); fprintf(file_1,s); fprintf(file_1,'.PROBE\n'); fprintf(file_1,'.END\n');
end
