%khwong modified 160826
%lecture notes: slide 5  of
% http://www.cse.cuhk.edu.hk/~khwong/www2/cmsc5707/5707_02_preprocess.pptx

function demo_time_framing()
%T= total sample number
%
%x=wavread('c:\sounds\A4_oboe.wav'); %assume sound is mono
% [Y,FS,NBITS]=WAVREAD(FILE)

%[x,fs,nbits]=wavread('A4_oboe.wav');%x=data,fs=sampling_rate,nbits=num_bits
%[x,fs,nbits]=wavread('s1.wav');%x=data,fs=sampling_rate,nbits=num_bits
[x,fs]=audioread('s1.wav');%x=data,fs=sampling_rate,nbits=num_bits
sprintf('The sampling frequncy is %d;\n',fs)
% N=segment_length
%x=orginal_data, N=segment length, s=segment_data, T=length of x
verbose=1 %1 will show detials, 0=ignor show details
N=1024
T=length(x)
si=1% init segment index
m=512 %non_overlapping samples

%in my lecture notes, q=si and the index is from 0, here it starts from 1.
%http://www.cse.cuhk.edu.hk/~khwong/www2/cmsc5707/5707_02_preprocess.ppt
N_si=round((T-N-m)/m); %estimate number of frames for this sound sequence
seg_begin=1 %init to 1
figure(1)
clf

for si=1:N_si
    %   start=1:N:(T-N)%loop start from 1,step N till samller or equal to (T-N)
    % ie start will be 1: 1+N;1+2N,.. till 1+KN equal to (T-N)
    si
    seg_begin
    seg_end=seg_begin+(N-1) %init to 1
    seg_length= seg_end-seg_begin+1
    s(:,si)=x(seg_begin:seg_end)'; % each column vector= one segment,
    if verbose
        size(s)
        '------------------'
        sprintf('Frame starts at sample %d;\nEnds at sample %d\n Frame  szie =%d',seg_begin,seg_end,N),
        sprintf('\n Current Frames index = %d, \nTotal frame size (samples) = %d,\n Non-overalapping samples =  %d.',si,T,m)
        %pause
    end
  
    
    %%%display wave
    figure(1)
    h211= subplot(2,1,1);

    plot(x)
    hold on
    line([ seg_begin  seg_begin],[0.5 -0.5] ,'color','r')
    line([  seg_begin+m seg_begin+m],[0.5 -0.5] ,'color','b')
    ylabel('enegy')
    legend('red = segment begins, blue=segment ends')
 
    h212=subplot(2,1,2)
    plot(s(:,si))
    hold on
     line([ 1 1],[0.5 -0.5] ,'color','b') %dummy line to make sure the sacle is right
    
    xlabel('time frames of the segment');
    ylabel('energy');
    title('plot of a time frame ')
    string1=sprintf('seg-begin=%d,seg-end=%d',seg_begin,seg_end)
        legend(string1)
     drawnow
     'Hit any key to continue to see the next frame'
     pause
     cla(h211)
     cla(h212)
     
       %preapre for teh next frame
    seg_begin=seg_begin+m;
end
'data of sagmentation result is in s, size of s is '
size(s)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Discrete Fourier transform
% by Project Nayuki, 2014. Public domain.
% http://www.nayuki.io/page/how-to-implement-the-discrete-fourier-transform
%

%
% This file contains multiple implementations.
% Before running the code, choose one and delete the rest.
%

% --------------------------------------------------------------------------------

%
% Computes the discrete Fourier transform (DFT) of the given vector.
% 'input' can be a row vector or a column vector.
% The returned output is the same type of vector with the same dimensions.
%
%function varargout = GuitarTuner(varargin)
function output = dft(input)
n = length(input);
output = zeros(size(input));
for k = 0 : n - 1  % For each output element
    s = 0;
    for t = 0 : n - 1  % For each input element
        s = s + input(t + 1) * exp(-2i * pi * t * k / n);
    end
    output(k + 1) = s;
end
size(output)
figure(1)
clf
plot(real(output));
real(output)
end
% --------------------------------------------------------------------------------

%
% (Alternate implementation using matrix arithmetic.)
% Computes the discrete Fourier transform (DFT) of the given vector.
% 'input' can be a row vector or a column vector.
% The returned output is the same type of vector with the same dimensions.
%
% function output = dft(input)
%   n = length(input);
%   matrix = exp(-2i * pi / n * [0:n-1]' * [0:n-1]);
%   if     all(size(input) == [1 n])  % Row vector
%     output = input * matrix;
%   elseif all(size(input) == [n 1])  % Column vector
%     output = matrix * input;
%   end
% end