Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. clear;
  2. clc;
  3. %% MATLAB
  4. %% read file
  5. %_________________________________________
  6. [y,fs]=audioread('UnchainMyHeart.wav');
  7. % audioread = read wav -file
  8. % y = contains the audio signal
  9. % fs = 44100
  10. % 'UnchainMyHeart' = name of the wav-file
  11. %_________________________________________
  12. %% PARAMETER FOR STFT
  13. %_________________________________________
  14. t_seg=0.03; % length of segment in ms
  15. fftlen = 4096; %FFT-Points
  16.  
  17. % Defining size of frequency bands
  18. f_low= 1:200; %lower frequencies
  19. f_medium= 201:600; %medium frequencies
  20. f_high= 601:1000; %higher frequencies
  21. %__________________________________________
  22. %% CODE
  23.  
  24. segl =floor(t_seg*fs);
  25. windowshift=segl/2;
  26. % defining the size of the window shift
  27. window=hann(segl);
  28. % apply hann function on segment length (30 ms)
  29. window=window.';
  30. % transpose vector
  31. si=1;
  32. % defining start index
  33. ei=segl;
  34. % defining end index
  35.  
  36. N=floor( length(y)/windowshift - 1);
  37. % Calculates the number, how often the window has to shift
  38. % until to length of the audio signal
  39.  
  40. f1=figure;
  41. % Generating new window
  42.  
  43. f=0:1:fftlen-1;
  44. f=f/fftlen*fs;
  45. % defining frequency vector
  46.  
  47. Ya=zeros(1,fftlen);
  48.  
  49. ValuesOfYc = NaN(1,N);
  50. ValuesOfYd = NaN(1,N);
  51. ValuesOfYe = NaN(1,N);
  52.  
  53. x =(1:N)*windowshift/fs;
  54. % defining x-axis
  55.  
  56. for m= 1:1:N
  57.  
  58. y_a = y(si:ei);
  59. % a segment is taken out from audio signal length(30ms)
  60. y_a= y_a.*window;
  61. % multiplying segment with window (hanning)
  62. Ya=fft(y_a, fftlen);
  63. % Applying fft on segment
  64. Yb=abs(Ya(1:end/2)).^2;
  65. % Squaring the magnitudes from one-sided spectrum
  66.  
  67. drawnow; % Updating the graphical values
  68.  
  69. figure(f1);
  70. % Showing the power values
  71.  
  72. %% frequency bands
  73.  
  74. y_low = Yb(f_low); % LOW frequency spectrum
  75. y_medium = Yb(f_medium); % MEDIUM frequency spectrum
  76. y_high = Yb(f_high); % HIGH frequency spectrum
  77.  
  78. Yc=sum(y_low);
  79. Yd=sum(y_medium);
  80. Ye=sum(y_high);
  81. % Summing all the power values from one frequency spectrum together
  82. % so you get one power value from one spectrum
  83. ValuesOfYc(m) = Yc;
  84. ValuesOfYd(m) = Yd;
  85. ValuesOfYe(m) = Ye;
  86. %Output values are being saved here, which are generated from the for
  87. %loop
  88. % m = start variable from for loop
  89.  
  90. subplot(2,1,1)
  91. p=plot(x,ValuesOfYc,'r-');%,x, ValuesOfYd,'g-', x, ValuesOfYe,'b-' );
  92. p(1).LineWidth =0.5;
  93. xlabel('time (Audio length)')
  94. ylabel('Power')
  95. grid on
  96.  
  97. subplot(2,1,2)
  98. p=plot(x, ValuesOfYd,'g-', x, ValuesOfYe,'b-' );
  99. p(1).LineWidth =0.5;
  100. xlabel('time (Audio length)')
  101. ylabel('Power')
  102. grid on
  103.  
  104. si=si+windowshift;
  105. % Updating start index
  106. ei=ei+windowshift;
  107. % Updating end index
  108.  
  109.  
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement