element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Documents Rescaling positive value
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: jpnbino
  • Date Created: 23 Aug 2018 10:05 PM Date Created
  • Last Updated Last Updated: 6 Oct 2021 9:42 PM
  • Views 516 views
  • Likes 0 likes
  • Comments 0 comments
Related
Recommended

Rescaling positive value

Edit: I know its boring to take someone else's code and set up an environment. For that, I've made the code available and ready to run in https://www.jdoodle.com/embed/v0/FeZ

 

Hey Folks, many times we need to translate values to different ranges so I wrote this piece of code. I hope you enjoy and give feedback and ideas to improve:

 

 

/**
  @Description
    This function performs a conversion from one scale X to scale Y. The relation between those scales is considered linear. Then, based on the parameters a known value x in scale X is converted to a value y in scale Y.
  @Preconditions
    None
  @Param
    x  - is the value to be converted to scale y
     xa - is the minimum scale X value.
     xb - is the maximum scale X value.
     ya - is the minimum scale Y value.
     yb - is the maximum scale Y value.
  @Returns
    The value converted from scale X to scale Y.
  @Comment
    This function uses the round half up method as an integer rounding method
  @Example
    <code>
     #define MIN_X 20
     #define MAX_X 50

     #define MIN_Y 1000
     #define MAX_Y 2000

     uint16_t y;
     uint16_t x = 30;

     y = Rescale_Value( x, MIN_X, MAX_X, MIN_Y, MAX_Y );

    </code>
*/

uint16_t Rescale_Value (uint16_t x , uint16_t xa, uint16_t xb, uint16_t ya, uint16_t yb )
{
/**
Algorithm:
1 - Find a linear equation;
2 - Calculate the equation considering rounding;

Explanation:
- A equation of the line correlates two different range of values. The equation
is given by the following formula,

y = ((yb - ya)*(x - xa))/(xb - xa)) + ya;

where,
(xa,ya) and (xb,yb) -> are known points in the line.

OBS: to complete understand solution search equation of the line theory.
Considerations:
y will be written as a/b + ya
where,
a = (yb - ya)*(x - xa)
b = (xb - xa)
*/
//test input condition
    if(x < xa)
    {
        x = xa;
    }
    else if ( x > xb)
    {
        x = xb;
    }
  
uint16_t y;

//multiplication of two 16bits gives a 32bits integer
uint32_t a;
uint16_t b;


a = (yb - ya)*(x - xa);
b = (xb - xa);

/**
In the matter of integers, calculation rounding must be considered.
Then, y is calculated using the round half up method
So, a/b is aproximated as (a/b + 1/2), but as calculation is performed by integers
1/2 = 0 (zero), then (a/b + 1/2) is rewritten as((a + b/2)/b).
*/
y = ((a + b/2)/b) + ya;

    return y ; 
}

  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube