Engineering and Automotive Applications

MATLAB is standard in automotive and engineering development:

Concept

MATLAB is standard in automotive and engineering development:

Measurement analysis Import logs (CSV, MDF/MF4 with Vehicle Network Toolbox mdf), decode CAN signals (DBC via canDatabase), plot and report

Fit sensor curves, generate lookup tables, optimize ECU maps (Model-Based Calibration Toolbox) Model-based design

Simulink models of plants and controllers, simulation, automatic C code generation (Embedded Coder) for ECUs, AUTOSAR support Battery systems

State-of-charge estimation (coulomb counting, Kalman filters), cell balancing analysis, thermal models (Simscape Battery)

Model-in-the-loop, software-in-the-loop, hardware-in-the-loop testing; Simulink Test, requirements traceability Signal processing

Vibration (NVH) analysis, filters, order analysis The example uses core MATLAB for four everyday tasks: decoding a CAN signal from raw bytes, coulomb-counting state of charge, calibrating a sensor from reference measurements, and detecting a sustained over-temperature condition.

Why It Matters

These are real daily tasks of test, calibration and controls engineers — and typical practical interview exercises for automotive MATLAB roles.

Syntax

bytes = hex2dec(reshape(hexText, 2, [])')';        % '1F40' -> [31 64]
raw = bytes(4) + bitshift(bytes(5), 8);            % little-endian 16-bit value
soc = soc0 + cumsum(current) * dt / capacity_As;   % coulomb counting

Example

30.1 CAN decoding, state of charge, calibration and fault detection

% ---- 1. Decode a CAN signal from raw bytes (little-endian, start bit 24, 16 bits, factor 0.125) ----
frames = {'FF7D7D401F000000', 'FF7D7D8025000000', 'FF7D7D002D000000'};
for k = 1:numel(frames)
    bytes = hex2dec(reshape(frames{k}, 2, [])')';          % '401F...' -> [64 31 ...]
    raw = bytes(4) + bitshift(bytes(5), 8);                % byte 3 is the LSB (bits 24..31)
    fprintf('frame %s -> raw %5d -> engine speed %.1f rpm\n', frames{k}, raw, raw * 0.125)
end

% ---- 2. Battery state of charge by coulomb counting ----
dt = 1;                                                    % s
current = [repmat(-20, 1, 600) repmat(5, 1, 300) zeros(1, 100)];   % A: discharge, charge, rest
capacity_As = 50 * 3600;                                   % 50 Ah
soc = 0.9 + cumsum(current) * dt / capacity_As;
fprintf('SoC: start 90.0%%, after discharge %.2f%%, after charge %.2f%%, final %.2f%%\n', ...
        soc(600) * 100, soc(900) * 100, soc(end) * 100)

% ---- 3. Sensor calibration from reference measurements ----
adc = [410 1230 2050 2870 3690];                           % raw ADC counts
ref = [0.50 1.50 2.50 3.50 4.50];                          % reference voltmeter (V)
cal = polyfit(adc, r
…

Expected Result

Bytes 3 and 4 (0-based) formed the little-endian raw value; 0x1F40 = 8000 → 1000 rpm with the factor 0.125.

The battery lost 20 A × 600 s = 12 000 As = 6.67 % of 50 Ah, then regained 5 A × 300 s = 1500 As.

The calibration fit maps ADC counts to volts with a tiny maximum error.

The three-consecutive-samples rule (implemented with conv) found the first sustained over-temperature event.

frame FF7D7D401F000000 -> raw  8000 -> engine speed 1000.0 rpm
frame FF7D7D8025000000 -> raw  9600 -> engine speed 1200.0 rpm
frame FF7D7D002D000000 -> raw 11520 -> engine speed 1440.0 rpm
SoC: start 90.0%, after discharge 83.33%, after charge 84.17%, final 84.17%
calibration: V = 1.219512e-03 * adc + 0.00000, max error 4.44e-16 V
three consecutive samples above 95 C start at sample 3 (values [96 97 99])

Common Mistakes

Byte order (Intel little-endian vs Motorola big-endian) in CAN decoding.

Sign convention of current (charge positive or negative) in SoC calculations.

Calibrating with too few or unevenly spread reference points.

Detecting faults on single samples without debouncing (false alarms).

Best Practices

Keep signal definitions (start bit, length, factor, offset) as data (a DBC file), not hard-coded numbers.

Document units in variable names.

Validate every processing step against a known reference.

Interview Relevance

High for automotive roles: "how would you decode a CAN signal in MATLAB?", "how do you estimate SoC?", "what is model-based design?", "what is MIL/SIL/HIL testing?".

Practice Questions

Decode a big-endian (Motorola) 12-bit signal from a CAN payload.

Add a charge efficiency factor of 0.98 to the SoC calculation.

Calibrate a pressure sensor with a quadratic fit and report the maximum error.

Debounce a fault flag: set after 3 consecutive faults, clear after 5 consecutive good samples.

Quick Quiz

Q1. (Multiple choice) In little-endian byte order, which byte is the least significant? A) The last one B) The first one C) The middle one D) It depends on the signal length Answer: B.

Q2. (Predict the output) Raw value 0x1F40 with factor 0.125 is how many rpm?

Answer: 1000 rpm (verified).

Q3. (True/False) Coulomb counting alone drifts over time because of current-sensor errors.

Answer: True — practical systems combine it with voltage-based corrections (e.g. Kalman filters).

Q4. (Find the bug) raw = bytes(4) * 256 + bytes(5); for a little-endian signal starting at byte 3.

Answer: The bytes are swapped: the low byte is bytes(4), the high byte bytes(5) → bytes(4) + bytes(5) * 256.

Coding Challenge

Decode the unsigned 8-bit coolant temperature (byte 0, offset −40) from the payloads {'82000000', '8C000000', '96000000'} and print the temperatures.

frames = {'82000000', '8C000000', '96000000'};
for k = 1:numel(frames)
    raw = hex2dec(frames{k}(1:2));
    fprintf('%s -> %d degC\n', frames{k}, raw - 40)
end