- refactored

This commit is contained in:
2022-06-30 13:32:40 +02:00
parent 77cd5261b1
commit 776932e5d1
144 changed files with 0 additions and 38 deletions
+55
View File
@@ -0,0 +1,55 @@
## Copyright (C) 2018 Jens Ahrensfeld
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{retval} =} sallenkey_eval (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn
## Author: Jens Ahrensfeld <ahrensfeld@w2ess001vm>
## Created: 2018-05-28
function sallenkey_eval (f_cut_Hz, Q)
w_cut = 2*pi*f_cut_Hz;
m = 1.0;
n = Q*(m*m + 1)/m;
R = 1.0e+3;
C = 1/(R*w_cut)
R1 = m*R
R2 = R/m
C1 = n*C
C2 = C/n
w0 = 0.1;
w1 = 2*pi*1e+6;
w_step = 10;
s = j*(w0:w_step:w1);
H_s = 1./(1 + C2*(R1 + R2)*s + C1*C2*R1*R2*s.^2);
semilogx(abs(s)/(2*pi), abs(H_s)); grid; xlabel('f/Hz'); ylabel('|H(j*2*pi*f)|')
endfunction
+30
View File
@@ -0,0 +1,30 @@
function [y, gain] = cic_filter(R, M, N, x)
% [y, gain] = cic_filter(R, M, N, x)
a_i = [1 -1];
b_i = 1;
a_c = 1;
b_c = [1 zeros(1, M-1) -1];
gain = (M*R)^N
k_i = 1/(M*R);
i_out = x;
% Integrator
for i=1:N,
i_out = k_i*filter(b_i, a_i, i_out);
end;
% Decimate
c_out = i_out(R:R:length(i_out));
% Comb
for i=1:N,
c_out = filter(b_c, a_c, c_out);
end;
y = c_out;
+23
View File
@@ -0,0 +1,23 @@
function cic_plot(M, N, R)
% cic_plot(M, N, R)
ns = 1000;
f = 2*(1:ns)/ns;
H = abs(sin(pi*M.*f)./sin(pi.*f/R)).^N;
subplot(2,1,1)
plot(f,H);
xlabel('f / fs');
ylabel('G');
grid;
Gain = (R*M)^N
BitGrowth = ceil(N*log2(R*M))
subplot(2,1,2)
plot(f,20*log10(H/Gain));
xlabel('f / fs');
ylabel('A/dB');
grid;
Atten_fs2 = 20*log10(H(ns/2)/Gain)
+71
View File
@@ -0,0 +1,71 @@
% COEF generiert IIR-Tiefpass Koeffizienten für geradzahlige Ordnung
% [b,a] = coef(fa,fg,Q,N)
%
% fa : Abtastfrequenz [Hz]
% fg : Grenzfrequenz [Hz] fg <= fa/2
% Q : Polgüte (Q=1.0 für Butterworth)
% N : Filterordnung (durch zwei teilbar)
% Type : 'Lowpass', 'Highpass', 'Bandpass', 'Bandstop'
%
function [b,a] = coef(fa,fg,Q,N,Type)
if (rem(N,2) > 0)
error('Filterordnung muss z.Zt. noch durch zwei teilbar sein!');
end;
if (strcmp(Type,'Bandstop'))
TF = 4;
end;
TF
sn = sin(2*pi*fg/fa);
cs = cos(2*pi*fg/fa);
ak = ones(N/2,3);
bk = ones(N/2,3);
a = 1;
b = 1;
if (strcmp(Type,'Lowpass'))
for n = 1:N/2,
Qp = 1/(2*sin(pi*(n-0.5)/N))
alpha = sn/(2*Q*Qp);
a0 = 1+alpha;
ak(n,2) = -2*cs/a0;
ak(n,3) = (1-alpha)/a0;
bk(n,1) = 0.5*(1-cs)/a0;
bk(n,2) = (1-cs)/a0;
bk(n,3) = 0.5*(1-cs)/a0;
a = conv(a,ak(n,(1:3)));
b = conv(b,bk(n,(1:3)));
end;
end;
if (strcmp(Type,'Highpass'))
for n = 1:N/2,
Qp = 1/(2*sin(pi*(n-0.5)/N))
alpha = sn/(2*Q*Qp);
a0 = 1+alpha;
ak(n,2) = -2*cs/a0;
ak(n,3) = (1-alpha)/a0;
bk(n,1) = 0.5*(1+cs)/a0;
bk(n,2) = -(1+cs)/a0;
bk(n,3) = 0.5*(1+cs)/a0;
a = conv(a,ak(n,(1:3)));
b = conv(b,bk(n,(1:3)));
end;
end;
if (strcmp(Type,'Bandpass'))
for n = 1:N/2,
Qp = 1/(2*sin(pi*(n-0.5)/N))
alpha = sn/(2*Q*Qp);
a0 = 1+alpha;
ak(n,2) = -2*cs/a0;
ak(n,3) = (1-alpha)/a0;
bk(n,1) = alpha/a0;
bk(n,2) = 0;
bk(n,3) = -alpha/a0;
a = conv(a,ak(n,(1:3)));
b = conv(b,bk(n,(1:3)));
end;
end;
+13
View File
@@ -0,0 +1,13 @@
% IIR-Tiefpass
% y = iirtp(fa,fg,Q,N,x)
%
% fa : Abtastfrequenz [Hz]
% fg : Grenzfrequenz [Hz] fg <= fa/2
% Q : Polgüte (Resonanz)
% N : Filterordnung (durch zwei teilbar)
% x : Eingangssignal
function y = iirtp(fa,fg,Q,N,x)
[b,a] = iirtpc(fa,fg,Q,N);
y = filter(b,a,x);
+13
View File
@@ -0,0 +1,13 @@
% IIR-Tiefpass
% [y,zf] = iirtp2(fa,fg,Q,N,x,zi)
%
% fa : Abtastfrequenz [Hz]
% fg : Grenzfrequenz [Hz] fg <= fa/2
% Q : Polgüte (Resonanz)
% N : Filterordnung (durch zwei teilbar)
% x : Eingangssignal
function [y,zf] = iirtp(fa,fg,Q,N,x,zi)
[b,a] = iirtpc(fa,fg,Q,N);
[y,zf] = filter(b,a,x,zi);
+33
View File
@@ -0,0 +1,33 @@
% IIRTPC generiert IIR-Tiefpass Koeffizienten für geradzahlige Ordnung
% [b,a] = iirtpc(fa,fg,Q,N)
%
% fa : Abtastfrequenz [Hz]
% fg : Grenzfrequenz [Hz] fg <= fa/2
% Q : Polgüte (Resonanz)
% N : Filterordnung (durch zwei teilbar)
%
function [b,a] = iirtpc(fa,fg,Q,N)
if (rem(N,2) > 0)
error('Filterordnung muss z.Zt. noch durch zwei teilbar sein!');
end;
sn = sin(2*pi*fg/fa);
cs = cos(2*pi*fg/fa);
ak = ones(N/2,3);
bk = ones(N/2,3);
a = 1;
b = 1;
for n = 1:N/2,
Qp = 1/(2*sin(pi*(n-0.5)/N));
alpha = sn/(2*Q*Qp);
a0 = 1+alpha;
ak(n,2) = -2*cs/a0;
ak(n,3) = (1-alpha)/a0;
bk(n,1) = 0.5*(1-cs)/a0;
bk(n,2) = (1-cs)/a0;
bk(n,3) = 0.5*(1-cs)/a0;
a = conv(a,ak(n,(1:3)));
b = conv(b,bk(n,(1:3)));
end;
+27
View File
@@ -0,0 +1,27 @@
% Erzeugt Filterkoeffizienten für FIR-Filter zur Eliminierung des Gleichanteils
% -----------------------------------------------------------------------------
%
% Aufruf
% Function [coeff] = undcc(N)
%
% Parameter:
% N : Bestimmt Grösse des Koeffizientenvektors
%
% Rückgabewerte:
% coeff : Koeffizientenvektor, Länge N
% -----------------------------------------------------------------------------
% Datum : 26.07.2001
% Autor : Jens Ahrensfeld
% Thema : Diplomarbeit
% Datei : undcc.m
% Benötigte Dateien:
%
% -----------------------------------------------------------------------------
function [coeff] = undcc(N)
% Ansatz: y = x - arith. Mittelwert(x)
coeff = [1.0-1/N ; -1/N*ones(N-1,1)];
% -----------------------------------------------------------------------------
% Ende undcc.m