SHARE
TWEET

Untitled

a guest Sep 17th, 2019 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clc;
  2. clear;
  3. close all;
  4.  
  5. % Defining variables
  6. filename = "enel420_grp_1.txt";
  7. fs = 1024; %Hz
  8. diff1 = 0;
  9. diff2 = 0;
  10. random_mutation = 0;
  11. %i = 0;
  12. z = 0;
  13. SNR_Min = 0;
  14.  
  15. %load the ECG data
  16. data = load(filename);
  17.  
  18. while (z < 30)
  19.     z = z + 1;
  20.     i = 0;
  21.    
  22.     b = [1, -1.59158, 1];
  23.     X = [1, -1.5, 1.0];
  24.     Y = [1, -1.6, 0.9];
  25.     Z = [1, -1.5, 0.95];
  26.  
  27.     % Applying each filter
  28.     IIR_filter_signal_X = filter(b, X, data);
  29.     IIR_filter_signal_Y = filter(b, Y, data);
  30.     IIR_filter_signal_Z = filter(b, Z, data);
  31.  
  32.     % Determining the SNR for each filter
  33.     snr_db_X = calculate_snr_after_filter(data, IIR_filter_signal_X);
  34.     snr_db_Y = calculate_snr_after_filter(data, IIR_filter_signal_Y);
  35.     snr_db_Z = calculate_snr_after_filter(data, IIR_filter_signal_Z);
  36.  
  37.     SNR_Max = max([snr_db_X, snr_db_Y, snr_db_Z]);
  38.    
  39.     while (SNR_Max < 15) && (i < 30)
  40.    
  41.     % Incremeting the counter
  42.     i = i + 1;
  43.    
  44.     % Removing weakest filter
  45.     SNR_Min = min([snr_db_X, snr_db_Y, snr_db_Z]);
  46.    
  47.     % Shift around so that Z always has the worse values
  48.     if (snr_db_X == SNR_Min)
  49.         X = Z;
  50.     end
  51.    
  52.     if (snr_db_Y == SNR_Min)
  53.         Y = Z;
  54.     end
  55.    
  56.     % Determining the new variables
  57.     diff1 = (X(2) - Y(2));
  58.     diff2 = (X(3) - Y(3));
  59.     if diff1 > 0.002
  60.         diff1 = 0.002;
  61.     end
  62.     if diff2 > 0.002
  63.         diff2 = 0.002;
  64.     end
  65.     random_mutation = (0.2 * rand(1,1)) - 0.1;
  66.    
  67.     X = [1, X(2) + diff1, X(3) + diff2];
  68.     Y = [1, Y(2) - diff1, Y(3) - diff2];
  69.     Z = [1, (X(2) + random_mutation + diff1), (random_mutation + X(3) + diff2)];
  70.          
  71.     % Applying each filter
  72.     IIR_filter_signal_X = filter(b, X, data);
  73.     IIR_filter_signal_Y = filter(b, Y, data);
  74.     IIR_filter_signal_Z = filter(b, Z, data);
  75.    
  76.     % Determining the SNR for each filter
  77.     snr_db_X = calculate_snr_after_filter(data, IIR_filter_signal_X);
  78.     snr_db_Y = calculate_snr_after_filter(data, IIR_filter_signal_Y);
  79.     snr_db_Z = calculate_snr_after_filter(data, IIR_filter_signal_Z);
  80.    
  81.     if (isnan(snr_db_X) == 1) || (snr_db_X < 0)
  82.         X = [1, -1.5, 1.0];
  83.         IIR_filter_signal_X = filter(b, X, data);
  84.         snr_db_X = calculate_snr_after_filter(data, IIR_filter_signal_X);
  85.     end
  86.     if (isnan(snr_db_Y) == 1) || (snr_db_Y < 0)
  87.         Y = [1, -1.6, 0.9];
  88.         IIR_filter_signal_X = filter(b, Y, data);
  89.         snr_db_Y = calculate_snr_after_filter(data, IIR_filter_signal_Y);
  90.     end
  91.     if (isnan(snr_db_Z) == 1) || (snr_db_Z < 0)
  92.         Z = [1, -1.5, 0.95];
  93.         IIR_filter_signal_X = filter(b, Z, data);
  94.         snr_db_Z = calculate_snr_after_filter(data, IIR_filter_signal_Z);
  95.     end
  96.        
  97.     SNR_Max = max([snr_db_X, snr_db_Y, snr_db_Z]);
  98.    
  99.     end
  100.     fprintf("%.2f, %.2f \n", SNR_Max, i);
  101. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top