Skip to content

Instantly share code, notes, and snippets.

@len0rd
Last active July 25, 2017 18:29
Show Gist options
  • Save len0rd/62085b6e14565d49fd1d47adeb46d77c to your computer and use it in GitHub Desktop.
Save len0rd/62085b6e14565d49fd1d47adeb46d77c to your computer and use it in GitHub Desktop.
Calculate altitude meters from pressure pascals (Pa->m)
%fast altitude calculation based on:
%https://en.wikipedia.org/wiki/Pressure_altitude
%(1-(millibars/1013.25)^(0.190284))*145366.45 = feet
%Bounds setup:
paMin = 69683;
paMax = 106597;
stepCount = 500; %number of values to plot
%setup pa vector
interval = round((paMax - paMin) / stepCount);
pa = paMin:interval:paMax; %pressures in pa
pa = [pa 106597];
%calculate result using the wiki equation:
fullResult = getAltFromPa(pa);
%csvwrite('rangeResult.txt', fullResult); %(opt) write results to file
%Get results for specific values. Right now I just use this as bounds...
specificValues = [69682 106597]
specificResult = getAltFromPa(specificValues)
%plot
figure('name', "Pressure to altitude")
clf
plot(pa, fullResult)
hold on
%(opt)load the lookup table from turbotrig and use it to calculate alt
%values corresponding to the pressure values in pa
%alt_lookup_table = csvread('currentValues.csv'); %read in the csv of currently used values for fast_alt
%currentlyUsed = calcLikeFastAltFunc(pa, alt_lookup_table);
%plot(pa, currentlyUsed, 'r')
%plot(specificValues, specificResult, 'r*');
xlabel('Pressure (Pa)')
ylabel('Altitude (m)')
hold off
function altMeters = getAltFromPa(paVec)
mbars = paVec .* .01; %convert to millibars
altFeet = (1-(mbars./1013.25).^(0.190284)).*145366.45;
altMeters = altFeet .* 0.3048;
end
function currentVec = calcLikeFastAltFunc(paVec, alt_lookup_table)
%this implementation is not ideal... just a quick copy pasta from
%turbotrig
paMin = 69683;
paMax = 106597;
t = length(paVec).*(paVec-paMin)./(paMax - paMin);
index = round(t) + 1;
dp = t - index;
currentVec = [];
for i=1:length(index)
if index(i) < (500)
toAdd = alt_lookup_table(index(i))/10 + dp(i)*(alt_lookup_table(index(i) + 1) - alt_lookup_table(index(i)))/10;
else
toAdd = alt_lookup_table(i)/10.0 + dp(i) * (alt_lookup_table(i) - alt_lookup_table(i - 1))/10.0;
end
currentVec = [currentVec toAdd];
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment