Advertisement
Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. function plot_Fourier1(inputSignal,fs,wlen)
  2.  
  3. % fs = sampling frequency
  4. % inputSignal = single column vector of numerical data
  5. % wlen = window length (recomended to be an integer power of 2)
  6. % shorter windows give higher resolution and are less affected by
  7. % high-frequency noise, they do however tend to loose the windowing effect
  8. % if they are chosen too small wrt the length of inputSignal
  9.  
  10. % To run the function on the desired EMG data, run this from the command
  11. % line:
  12. % >> plot_Fourier1(Kanaal1mVolt,1000,512); (don't forget to import
  13. % Kanaal1mVolt)
  14.  
  15.  
  16. signal = inputSignal;
  17.  
  18. x = signal(1: (length(signal)-1)); % exclude last value (NaN) because signal processor can't deal with that...
  19. time = 1 : (length(signal)-1);
  20. time = time.';
  21. time = time/1000; %divide by sampling frequency
  22.  
  23. %%%%%%%%%%%%%%%%%%%%%%Plot Raw Data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24.  
  25. %define analysis parameters for stft
  26. xlen = length(x); % length of the signal
  27. hop = wlen/4; % hop size (recomended to be power of 2)
  28. nfft = 2^nextpow2(xlen);
  29.  
  30. figure(1);
  31. plot(time,x);
  32. set(gca, 'FontName', 'Arial', 'FontSize', 14);
  33. xlabel('Time, s');
  34. ylabel('Amplitude, mV');
  35. title('Time Domain Signal - Raw EMG');
  36.  
  37. % apply short-time Fourier transform for spectral analysis
  38.  
  39. % number of fft points (recomended to be power of 2)
  40. % perform STFT
  41. [S, f, t] = stft(x, wlen, hop, nfft, fs);
  42.  
  43. K = sum(hamming(wlen, 'periodic'))/wlen;
  44. % take the amplitude of fft(x) and scale it, so not to be a
  45. % function of the length of the window and its coherent amplification
  46. S = abs(S)/wlen/K;
  47.  
  48. if rem(nfft, 2)
  49. S(2:end, :) = S(2:end, :).*2;
  50. else
  51. S(2:end-1, :) = S(2:end-1, :).*2;
  52. end
  53.  
  54. % convert amplitude spectrum to dB (min = -120 dB)
  55. S = 20*log10(S + 1e-6);
  56.  
  57. % plot the spectrogram
  58.  
  59. figure(2);
  60. surf(t, f, S);
  61. colormap(jet);
  62. shading interp;
  63. axis tight;
  64. box on;
  65. view(0, 90);
  66. handl = colorbar;
  67. set(handl, 'FontName', 'Arial', 'FontSize', 14,'Colormap',jet);
  68. xlabel('Time, s');
  69. ylabel('Frequency, Hz');
  70. title('Spectogram of Raw EMG');
  71. ylabel(handl, 'Magnitude, dB');
  72.  
  73.  
  74. %%%%%%%%%%%%%%%%%%Filter the Data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  75.  
  76. %notch filter to remove noise from 50 Hz
  77. % Design a filter with a Q-factor of Q=35 to remove a 50 Hz tone from
  78. % system running at 1000 Hz.
  79. Wo = 50/(fs/2); BW = Wo/35;
  80. [b,a] = iirnotch(Wo,BW);
  81. x = filter(b,a,x); %use filtfilt to avoid phase shifts?
  82.  
  83. % Get rid of the DC component (hp filter at 350 Hz)
  84. fc=350;
  85. [b,a] = butter(6,2*fc/fs,'high');
  86. x = filter(b,a,x);
  87.  
  88.  
  89. %filter out noise from the electrical setup, including higher harmonics
  90. %of the AC supply (aka evrthg over 450 Hz)
  91. fc=450;
  92. [b,a] = butter(6,2*fc/fs,'low');
  93. x = filter(b,a,x);
  94.  
  95.  
  96. % number of fft points (recomended to be power of 2)
  97. % perform STFT
  98. [S1, f, t] = stft(x, wlen, hop, nfft, fs);
  99. % define the coherent amplification of the window
  100. %'periodic' — This option is useful for spectral analysis because it
  101. % enables a windowed signal to have the perfect periodic extension implicit
  102. % in the discrete Fourier transform. When 'periodic' is specified, hamming
  103. % computes a window of length L + 1 and returns the first L points.
  104.  
  105. K = sum(hamming(wlen, 'periodic'))/wlen;
  106. % take the amplitude of fft(x) and scale it, so not to be a
  107. % function of the length of the window and its coherent amplification
  108. S1 = abs(S1)/wlen/K;
  109.  
  110.  
  111. if rem(nfft, 2)
  112. S1(2:end, :) = S1(2:end, :).*2;
  113. else
  114. S1(2:end-1, :) = S1(2:end-1, :).*2;
  115. end
  116.  
  117. % convert amplitude spectrum to dB (min = -120 dB)
  118. S1 = 20*log10(S1 + 1e-6);
  119.  
  120. % plot the spectrogram
  121. figure(3);
  122. surf(t, f, S1);
  123. colormap(jet);
  124. shading interp;
  125. axis tight;
  126. box on;
  127. view(0, 90);
  128. handl = colorbar;
  129. set(handl, 'FontName', 'Arial', 'FontSize', 14, 'Colormap', jet);
  130. xlabel('Time, s');
  131. ylabel('Frequency, Hz');
  132. title('Spectrogram of Filtered EMG');
  133. ylabel(handl, 'Magnitude, dB');
  134. set(gca,'Ylim',[0 550]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement