-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfir_fitness_function.m
31 lines (24 loc) · 1.09 KB
/
fir_fitness_function.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function fitness = fir_fitness_function(coefficients, ideal_coefficients, Fp, Fs, Rp, As, N)
% Construct the full set of coefficients for Type-1 FIR filter
% Adjust the order to ensure it's even
if mod(N, 2) ~= 0
N = N + 1;
end
b = [coefficients, fliplr(coefficients(1:end-2))];
% Frequency response
[H, f] = freqz(b, 1, 1024, 'whole'); % Full frequency response
H_dB = 20*log10(abs(H));
% Normalize the frequency vector
f = f/pi;
% Define frequency bands
passband = f <= Fp;
stopband = f >= Fs;
% Calculate passband ripple and stopband attenuation
passband_ripple = max(H_dB(passband)) - min(H_dB(passband));
stopband_min_attenuation = min(H_dB(stopband));
% Error between GA-generated coefficients and ideal sinc coefficients
coeff_error = sum((b - ideal_coefficients).^2);
% Fitness function: a combination of ripple and attenuation with penalties
% fitness = passband_ripple + abs(stopband_min_attenuation + As) + passband_penalty + stopband_penalty ;
fitness = 100*coeff_error;
end