Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear;
- clc;
- %% MATLAB
- %% read file
- %_________________________________________
- [y,fs]=audioread('UnchainMyHeart.wav');
- % audioread = read wav -file
- % y = contains the audio signal
- % fs = 44100
- % 'UnchainMyHeart' = name of the wav-file
- %_________________________________________
- %% PARAMETER FOR STFT
- %_________________________________________
- t_seg=0.03; % length of segment in ms
- fftlen = 4096; %FFT-Points
- % Defining size of frequency bands
- f_low= 1:200; %lower frequencies
- f_medium= 201:600; %medium frequencies
- f_high= 601:1000; %higher frequencies
- %__________________________________________
- %% CODE
- segl =floor(t_seg*fs);
- windowshift=segl/2;
- % defining the size of the window shift
- window=hann(segl);
- % apply hann function on segment length (30 ms)
- window=window.';
- % transpose vector
- si=1;
- % defining start index
- ei=segl;
- % defining end index
- N=floor( length(y)/windowshift - 1);
- % Calculates the number, how often the window has to shift
- % until to length of the audio signal
- f1=figure;
- % Generating new window
- f=0:1:fftlen-1;
- f=f/fftlen*fs;
- % defining frequency vector
- Ya=zeros(1,fftlen);
- ValuesOfYc = NaN(1,N);
- ValuesOfYd = NaN(1,N);
- ValuesOfYe = NaN(1,N);
- x =(1:N)*windowshift/fs;
- % defining x-axis
- for m= 1:1:N
- y_a = y(si:ei);
- % a segment is taken out from audio signal length(30ms)
- y_a= y_a.*window;
- % multiplying segment with window (hanning)
- Ya=fft(y_a, fftlen);
- % Applying fft on segment
- Yb=abs(Ya(1:end/2)).^2;
- % Squaring the magnitudes from one-sided spectrum
- drawnow; % Updating the graphical values
- figure(f1);
- % Showing the power values
- %% frequency bands
- y_low = Yb(f_low); % LOW frequency spectrum
- y_medium = Yb(f_medium); % MEDIUM frequency spectrum
- y_high = Yb(f_high); % HIGH frequency spectrum
- Yc=sum(y_low);
- Yd=sum(y_medium);
- Ye=sum(y_high);
- % Summing all the power values from one frequency spectrum together
- % so you get one power value from one spectrum
- ValuesOfYc(m) = Yc;
- ValuesOfYd(m) = Yd;
- ValuesOfYe(m) = Ye;
- %Output values are being saved here, which are generated from the for
- %loop
- % m = start variable from for loop
- subplot(2,1,1)
- p=plot(x,ValuesOfYc,'r-');%,x, ValuesOfYd,'g-', x, ValuesOfYe,'b-' );
- p(1).LineWidth =0.5;
- xlabel('time (Audio length)')
- ylabel('Power')
- grid on
- subplot(2,1,2)
- p=plot(x, ValuesOfYd,'g-', x, ValuesOfYe,'b-' );
- p(1).LineWidth =0.5;
- xlabel('time (Audio length)')
- ylabel('Power')
- grid on
- si=si+windowshift;
- % Updating start index
- ei=ei+windowshift;
- % Updating end index
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement