function [ newPattern ] = powerProfile( pattern )
%powerProfile( pattern ) Calculates the power requirement of a driving
%profile
%
%  Given a pattern structure with two vectors, pattern.t for time points
%  and pattern.v for corresponding speed in km/h, a new structure is
%  returned, newPattern, with both .t and .v (in m/s), plus other fields,
%  newPattern.roll for the power absorbed by rolling resistance,
%  newPattern.air for the power absorbed by air resistance,
%  newPattern.a for the acceleration, newPattern.kinetic for kinetic
%  energy power requirements, newPattern.w for the wheel angular
%  velocity, newPattern.torque for the wheel torque, newPattern.power 
%  for the total power absorbed.
%  Typical parameter values are assumed.


m   = 1000  ;       % Vehicle mass, kg
A   = 1.5   ;       % Vehicle front area, m²
CRF = 0.02  ;       % Rolling resistance coefficient
Cd  = 0.4   ;       % Air resistance constant
g   = 9.81  ;       % Gravity acceleration, m/s²
rho = 1.177 ;       % Air density, kg/m³

newPattern = pattern ;

newPattern.v = newPattern.v / 3.6 ;     % Converting to m/s

% Power dissipated by rolling resistance
newPattern.roll = m * g * CRF * newPattern.v ;

% Power dissipated by air resistance
newPattern.air = 0.5 * rho * ( newPattern.v ) .^ 3 * Cd * A ;

% Acceleration
newPattern.a = zeros( size( newPattern.v ) ) ;

newPattern.a(1:end-1) = ( newPattern.v(2:end) - newPattern.v(1:end-1) ) ./...
                   ( newPattern.t(2:end) - newPattern.t(1:end-1) ) ;

newPattern.a(end) = ( newPattern.v(end) - newPattern.v(end-1) ) /...
                    ( newPattern.t(end) - newPattern.t(end-1) ) ;

% Power spent in kinetic energy change due to acceleration
newPattern.kinetic = m * newPattern.v .* newPattern.a ;

% Total power requirement
newPattern.power = newPattern.roll + newPattern.air + newPattern.kinetic ;
newPattern.power(find ( newPattern.power < 0 )) = 0 ;

% Fast fourier transform of velocity
newPattern.v_fou = fft( newPattern.v ) ;
newPattern.v_fou = newPattern.v_fou( 1:floor( length( newPattern.v_fou )/2 ) ) ;
newPattern.v_w = ( 1:length( newPattern.v_fou ) ) / ( 2 * newPattern.t( end ) ) * 2 * pi ;

% Fast fourier transform of required power/velocity (i.e. force)
rollForce  = m * g * CRF * sign( newPattern.v ) ;
airForce   = 0.5 * rho * ( newPattern.v ) .^ 2 * Cd * A ;
accelForce = m * newPattern.a ;

newPattern.F = rollForce + airForce + accelForce ;
newPattern.F(find ( newPattern.F < 0 )) = 0 ;   % Remove <0 elements as they do not influence the fuel cell
newPattern.F_fou = fft( newPattern.F ) ;
newPattern.F_fou = newPattern.F_fou( 1:floor( length( newPattern.F_fou )/2 ) ) ;
newPattern.F_w = ( 1:length( newPattern.F_fou ) ) / ( 2 * newPattern.t( end ) ) * 2 * pi ;
