גרפים: הבדלים בין גרסאות

מתוך מעבדת מבוא בחשמל
קפיצה אל: ניווט, חיפוש
(יצירת הדף עבור קידוד של גרפים)
 
(הוספת ייצוג ספקטראלי)
שורה 40: שורה 40:
  
 
set(gcf, 'Position', [600, 80, 800, 900]);
 
set(gcf, 'Position', [600, 80, 800, 900]);
 +
</syntaxhighlight>
 +
 +
= ייצוג ספקטראלי של גלים יסודיים =
 +
יש לשנות את המשתנה ''wave'' לערכים של בין 1 ל-4 כדי לקבל את ארבעת הגלים השונים:
 +
# סינוסי
 +
# ריבועי
 +
# שן-מסור
 +
# משולש
 +
 +
[[File:FFTSawtooth.png|thumb|right|upright=1.5|<figure id="fig:fft_sawtooth"><caption>גל שן-מסור והייצוג הספקטראלי שלו</caption></figure>]]
 +
<syntaxhighlight lang="matlab" line='line'>
 +
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 +
%%% FFT based on                                      %%%
 +
%%% https://www.mathworks.com/help/matlab/ref/fft.html %%%
 +
%%% with a few modifications                          %%%
 +
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 +
freq = 1000; % frequency
 +
Fs = freq*20; % Sampling frequency                   
 +
T = 1/Fs; % Sampling period     
 +
L = 200; % Length of signal
 +
t = (0:L-1)*T; % Time vector
 +
 +
% three different waves
 +
RMS_Amplitude = 1;
 +
x = 2*pi*freq*t;
 +
Names = {'Sine', 'Square', 'Sawtooth', 'Triangle'};
 +
Amplitudes = [sqrt(2), 1, sqrt(3), sqrt(3)] * RMS_Amplitude;
 +
Waves = {sin(x), square(x), sawtooth(x), sawtooth(x,0.5)};
 +
wave = 1; % actual wave to display
 +
 +
S=Amplitudes(wave)*Waves{wave};
 +
f = Fs*(0:(L/2))/L;
 +
Y = fft(S);
 +
P2 = abs(Y/L);
 +
P1 = P2(1:L/2+1);
 +
P1(2:end-1) = 2*P1(2:end-1);
 +
 +
figure('Name','FFT example');
 +
set(gcf, 'Position', [600, 80, 800, 900]);
 +
ax=subplot(211);
 +
plot(t,S);
 +
title(['Original ',Names{wave},' wave']);
 +
ylabel('Amplitude [V]');
 +
xlabel('Time [S]');
 +
if max(ax.YLim)==max(S)
 +
    ax.YLim=ax.YLim*1.1;
 +
end
 +
ax=subplot(212);
 +
P1_RMS=P1/sqrt(2);
 +
plot(f,P1_RMS);
 +
title(['FFT of the ',Names{wave},' wave']);
 +
xlabel('f (Hz)');
 +
ylabel('Amplitude [RMS V]');
 +
if max(ax.YLim)==max(P1_RMS)
 +
    ax.YLim=ax.YLim*1.1;
 +
end
 
</syntaxhighlight>
 
</syntaxhighlight>

גרסה מתאריך 08:58, 16 באוקטובר 2017

1 גרף הדגמת FFT ו-THD

איור 1: הדגמה של THD
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%% FFT with THD example                                        %%%
  3. %%% based on https://www.mathworks.com/help/matlab/ref/fft.html %%%
  4. %%% with a few modifications                                    %%%
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. Fs = 1000; % Sampling frequency                    
  7. T = 1/Fs; % Sampling period       
  8. L = 300; % Length of signal
  9. t = (0:L-1)*T; % Time vector
  10. freq = 50; % frequency
  11. S1 = 5*sin(2*pi*freq*t); % 1st (base) harmony
  12. S2 = 2*sin(2*pi*3*freq*t); % 2nd harmony distortion
  13. S = S1 + S2;
  14. f = Fs*(0:(L/2))/L;
  15. Y = fft(S);
  16. P2 = abs(Y/L);
  17. P1 = P2(1:L/2+1);
  18. P1(2:end-1) = 2*P1(2:end-1);
  19.  
  20. figure('Name','THD example');
  21. subplot(311);
  22. plot(t,S1,t,S2);
  23. title('Two pure sine waves');
  24. ylabel('Amplitude [V]');
  25. xlabel('Time [S]');
  26. legend(['1st harmony: ',num2str(freq),' Hz'],['2nd harmony: ',num2str(3*freq),' Hz']);
  27. subplot(312);
  28. plot(t,S);
  29. title(['Addition of the above two sine waves with frequencies of ',num2str(freq),' and ',num2str(3*freq),' Hz']);
  30. ylabel('Amplitude [V]');
  31. xlabel('Time [S]');
  32. subplot(313);
  33. plot(f,P1);
  34. title('Single-Sided Amplitude Spectrum of 1st (base) harmony + 2nd harmony');
  35. xlabel('f (Hz)');
  36. ylabel('Amplitude [V]');
  37.  
  38. set(gcf, 'Position', [600, 80, 800, 900]);

2 ייצוג ספקטראלי של גלים יסודיים

יש לשנות את המשתנה wave לערכים של בין 1 ל-4 כדי לקבל את ארבעת הגלים השונים:

  1. סינוסי
  2. ריבועי
  3. שן-מסור
  4. משולש
איור 2: גל שן-מסור והייצוג הספקטראלי שלו
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%% FFT based on                                       %%%
  3. %%% https://www.mathworks.com/help/matlab/ref/fft.html %%%
  4. %%% with a few modifications                           %%%
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. freq = 1000; % frequency
  7. Fs = freq*20; % Sampling frequency                    
  8. T = 1/Fs; % Sampling period       
  9. L = 200; % Length of signal
  10. t = (0:L-1)*T; % Time vector
  11.  
  12. % three different waves
  13. RMS_Amplitude = 1;
  14. x = 2*pi*freq*t;
  15. Names = {'Sine', 'Square', 'Sawtooth', 'Triangle'};
  16. Amplitudes = [sqrt(2), 1, sqrt(3), sqrt(3)] * RMS_Amplitude;
  17. Waves = {sin(x), square(x), sawtooth(x), sawtooth(x,0.5)};
  18. wave = 1; % actual wave to display
  19.  
  20. S=Amplitudes(wave)*Waves{wave};
  21. f = Fs*(0:(L/2))/L;
  22. Y = fft(S);
  23. P2 = abs(Y/L);
  24. P1 = P2(1:L/2+1);
  25. P1(2:end-1) = 2*P1(2:end-1);
  26.  
  27. figure('Name','FFT example');
  28. set(gcf, 'Position', [600, 80, 800, 900]);
  29. ax=subplot(211);
  30. plot(t,S);
  31. title(['Original ',Names{wave},' wave']);
  32. ylabel('Amplitude [V]');
  33. xlabel('Time [S]');
  34. if max(ax.YLim)==max(S)
  35.     ax.YLim=ax.YLim*1.1;
  36. end
  37. ax=subplot(212);
  38. P1_RMS=P1/sqrt(2);
  39. plot(f,P1_RMS);
  40. title(['FFT of the ',Names{wave},' wave']);
  41. xlabel('f (Hz)');
  42. ylabel('Amplitude [RMS V]');
  43. if max(ax.YLim)==max(P1_RMS)
  44.     ax.YLim=ax.YLim*1.1;
  45. end