%khwong modified 160826
%
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]=audioread('s1.wav');%x=data,fs=sampling_rate,nbits=num_bits
sprintf('The sampling frequncy is %d;',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)
    h311= subplot(3,1,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')
    xlabel('time in samples')
    ylabel('pressure or voltage')
    legend('red = segment begins, blue=segment ends')
    title('plot of a time domian signal ')
    %---------  2 -------------------------------------
    h312=subplot(3,1,2)
    plot(s(:,si))
  %  xlabel('time in samples')
    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 in samples');
    ylabel('pressure or voltage')
     string1=sprintf('seg-begin=%d,seg-end=%d',seg_begin,seg_end)
    legend(string1)
    title('plot of a time domian signal ')
   
    drawnow
    
    %preapre for teh next frame
    seg_begin=seg_begin+m;
    %---------  3 -------------------------------------
    h313=subplot(3,1,3)
    
    %NFFT = 2^nextpow2(L); % Next power of 2 from length of y
    clear Fs Y f
    Fs=fs
    NFFT=1024;
    Y = fft(s(:,si),NFFT);
    f = Fs/2*linspace(0,1,NFFT/2+1);
    
    % Plot single-sided amplitude spectrum.
    plot(f,2*abs(Y(1:NFFT/2+1)))
  
    xlabel('Frequency (Hz)')
    ylabel('|Y(f)| power spectrum')
    title('Single-Sided Amplitude Spectrum of y(t) of a segment')
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
    p=10
    [a,g]=lpc(s(:,si),p); %[a,g] = lpc(x,p)%x=signal, p=order of lpc
    'Now display the result of the lPC=a, error=g for this segment'
    a
    g
    'Hit any kit to continue!'
    pause
    cla(h311)
    cla(h312)
    cla(h313)
    
    'testing fft'
    %size(s)
    %pause
    
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