Guest User

Untitled

a guest
Oct 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. clear; close all
  2.  
  3. %% Generate signal
  4.  
  5. nharodd = 100;
  6. Fs = 1E3;
  7. t = 0:1/Fs:10-1/Fs;
  8. X = nan(length(t), nharodd);
  9. for i = 1:nharodd
  10. X(:, i) = 1/(2*i-1)*sin(2*pi*(2*i-1)*5*t);
  11. end
  12. x = sum(X, 2);
  13. figure(1); subplot(3, 1, 1); plot(t, x)
  14. xlabel('Time (s)'); ylabel('B (T)')
  15.  
  16. Y = fftshift(fft(x));
  17. L = size(Y, 1);
  18. P1 = 2*abs(Y/L);
  19. P1((length(P1))/2+1) = P1((length(P1))/2+1)/2;
  20. phs = angle(Y); phs(P1 < 1E-4) = 0;
  21. f = Fs*(-L/2:(L/2)-1)/L;
  22.  
  23. subplot(3, 1, 2); plot(f, P1, 'r')
  24. xlabel('Frequency (Hz)'); ylabel('B (T)');
  25. subplot(3, 1, 3); plot(f, phs, 'r')
  26. xlabel('Frequency (Hz)'); ylabel('Phase (radians)')
  27.  
  28. %% Modify signal by adding some even harmonic components
  29.  
  30. nhareven = 100;
  31. Z = nan(length(t), nhareven);
  32. for i = 0:100
  33. for j = 1:nhareven
  34. Z(:, j) = 1/(2*j)*sin(2*pi*(2*j)*5*t);
  35. end
  36. z = x + i*0.01*sum(Z, 2);
  37. figure(2); subplot(3, 1, 1); plot(t, z)
  38. xlabel('Time (s)'); ylabel('B (T)')
  39. title(sprintf('Relative strength of even harmonics: %2.2f', 0.01*i))
  40.  
  41. Y = fftshift(fft(z));
  42. L = size(Y, 1);
  43. P1 = 2*abs(Y/L);
  44. P1((length(P1))/2+1) = P1((length(P1))/2+1)/2;
  45. phs = angle(Y); phs(P1 < 1E-4) = 0;
  46. f = Fs*(-L/2:(L/2)-1)/L;
  47.  
  48. subplot(3, 1, 2); plot(f, P1, 'r')
  49. xlabel('Frequency (Hz)'); ylabel('B (T)');
  50. subplot(3, 1, 3); plot(f, phs, 'r')
  51. xlabel('Frequency (Hz)'); ylabel('Phase (radians)')
  52.  
  53. pause(0.01)
  54. end
  55.  
  56. figure; yyaxis left; plot(t, [0; diff(z)*Fs]);
  57. xlabel('Time (s)'); ylabel('H (A/m)')
  58. yyaxis right; plot(t, cumtrapz([0; diff(z)*Fs]))
  59. ylabel('B-hat (T)')
  60.  
  61. %% Add DC bias to B
  62.  
  63. q = x + 0.5;
  64.  
  65. figure; subplot(3, 1, 1); plot(t, q);
  66. xlabel('Time (s)'); ylabel('B (T)')
  67.  
  68. Y = fftshift(fft(q));
  69. L = size(Y, 1);
  70. P1 = 2*abs(Y/L);
  71. P1((length(P1))/2+1) = P1((length(P1))/2+1)/2;
  72. phs = angle(Y); phs(P1 < 1E-4) = 0;
  73. f = Fs*(-L/2:(L/2)-1)/L;
  74.  
  75. subplot(3, 1, 2); plot(f, P1, 'r')
  76. xlabel('Frequency (Hz)'); ylabel('B (T)');
  77. subplot(3, 1, 3); plot(f, phs, 'r')
  78. xlabel('Frequency (Hz)'); ylabel('Phase (radians)')
  79.  
  80. %% Visualize hysteresis loops for a sinusoidal input
  81.  
  82. % H leads B
  83. h = sin(2*pi*5*t - pi/10);
  84.  
  85. figure; plot(h, x)
  86. hold on; plot(h, z)
  87. plot(h, q)
  88. xlabel('H (A/m)'); ylabel('B(T)')
Add Comment
Please, Sign In to add comment