Advertisement
Staryy

Project_scripts

Jan 10th, 2020
1,643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.92 KB | None | 0 0
  1. % Essential parameters
  2. fs = 240;
  3. Ts = 1/fs;
  4. data = load ('ECG1.txt');
  5.  
  6. % Time vector
  7. t = 0 : Ts : ((length(data)-1)*Ts);
  8.  
  9. % Noise
  10. fNoise = 50;    % Frequency [Hz]
  11. aNoise = 0.25;  % Amplitude
  12. noise  = aNoise*sin(2*pi.*t.*fNoise);
  13.  
  14. % Loading data from the file
  15.  
  16. x = data(:, 1)';
  17. y = data(:, 2)';
  18.  
  19. %Addition of the noise to the original signal
  20.  
  21. signalNoise = y + noise;
  22.  
  23. % Plot of noisy signal
  24.  
  25. figure(1)
  26. subplot(2,1,1)
  27. plot(x, signalNoise, ';Signal with noise ;');
  28. title('ECG Signal', 'FontSize', 20);
  29. xlabel('Time', 'FontSize', 20);
  30. ylabel('Voltage', 'FontSize', 20);
  31. grid on;
  32. subplot(2,1,2)
  33. signalNoise_fft = fft(signalNoise);
  34. stem(abs(signalNoise_fft)(1:250))
  35. title('ECG Signal', 'FontSize', 20);
  36. xlabel('Frequency', 'FontSize', 20);
  37. ylabel('Magnitude', 'FontSize', 20);
  38. set(gca, 'xTickLabel', 0:24:120);
  39.  
  40. % Plot of original signal
  41.  
  42. figure(2)
  43. subplot(2,1,1)
  44. plot(x, y, ';Original signal;');
  45. title('ECG Signal', 'FontSize', 20);
  46. xlabel('Time', 'FontSize', 20);
  47. ylabel('Voltage', 'FontSize', 20);
  48. grid on;
  49. subplot(2,1,2)
  50. y_fft = fft(y);
  51. stem(abs(y_fft)(1:250))
  52. title('ECG Signal', 'FontSize', 20);
  53. xlabel('Frequency', 'FontSize', 20);
  54. ylabel('Magnitude', 'FontSize', 20);
  55. set(gca, 'xTickLabel', 0:24:120);
  56.  
  57. % Comparison of noisy and original signal
  58.  
  59. figure(3)
  60. plot(x, signalNoise, ';Signal with noise ;');
  61. hold on
  62. plot(x, y, ';Original signal;');
  63. title('ECG Signal', 'FontSize', 20);
  64. xlabel('Time', 'FontSize', 20);
  65. ylabel('Voltage', 'FontSize', 20);
  66. grid on;
  67. hold off;
  68.  
  69. % Plot of Notch filter
  70.  
  71. figure(4)
  72. fs2 = fs / 2;
  73. [b,a] = pei_tseng_notch (50 / fs2, 2/fs2);
  74. filtered = filter(b,a,signalNoise);
  75. subplot (2,1,1)
  76. plot(filtered, ';Notch filter;');
  77. title('ECG Signal', 'FontSize', 20);
  78. xlabel('Time', 'FontSize', 20);
  79. ylabel('Voltage', 'FontSize', 20);
  80. grid on;
  81. subplot (2,1,2)
  82. notch_fft = fft(filtered);
  83. stem (abs(notch_fft)(1:250))
  84. title('ECG Signal', 'FontSize', 20);
  85. xlabel('Frequency', 'FontSize', 20);
  86. ylabel('Magnitude', 'FontSize', 20);
  87. set(gca, 'xTickLabel', 0:24:120);
  88.  
  89. % Plot of FIR filter
  90.  
  91. figure(5)
  92. f1 = 50/(fs/2) - 0.03;
  93. f2 = 50/(fs/2) + 0.03;
  94. c = fir1(100,[f1 f2], "stop" );
  95. filtered2 = filter(c,1,signalNoise);
  96. subplot (2,1,1)
  97. plot(filtered2(1,50:500), ';FIR filter;');
  98. title('ECG Signal', 'FontSize', 20);
  99. xlabel('Time', 'FontSize', 20);
  100. ylabel('Voltage', 'FontSize', 20);
  101. grid on;
  102. subplot (2,1,2)
  103. fir_fft = fft (filtered2);
  104. stem (abs(fir_fft)(1:250));
  105. title('ECG Signal', 'FontSize', 20);
  106. xlabel('Frequency', 'FontSize', 20);
  107. ylabel('Magnitude', 'FontSize', 20);
  108. set(gca, 'xTickLabel', 0:24:120);
  109.  
  110. % Comparison of Notch and FIR filter
  111.  
  112. figure(6)
  113. plot(filtered2(1,50:500), ';FIR filter;');
  114. title('ECG Signal', 'FontSize', 20);
  115. xlabel('Time', 'FontSize', 20);
  116. ylabel('Voltage', 'FontSize', 20);
  117. grid on;
  118. hold on
  119. plot(filtered, ';Notch filter;');
  120. title('ECG Signal', 'FontSize', 20);
  121. xlabel('Time', 'FontSize', 20);
  122. ylabel('Voltage', 'FontSize', 20);
  123. hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement