

function [accel, t] = generate_bearing_signal(varargin)
% GENERATE_BEARING_SIGNAL  Synthesize a raw accelerometer time history for
%   machine dynamics / envelope analysis demonstration.
%
%   The signal models the "raw" trace in a classic envelope (demodulation)
%   analysis figure: a dominant low-frequency machine vibration (1X shaft
%   running speed plus a harmonic) with periodic, exponentially decaying
%   high-frequency impact bursts superimposed. Each burst represents a
%   rolling element striking a localized bearing defect and ringing the
%   sensor/structure resonance. Broadband noise is added for realism.
%
%   [accel, t] = generate_bearing_signal()
%   [accel, t] = generate_bearing_signal('Name', Value, ...)
%
%   Name-Value options (all optional; representative defaults shown):
%     'Fs'          Sample rate, Hz                         (default 50000)
%     'Duration'    Record length, sec                      (default 1.0)
%     'ShaftRPM'    Shaft running speed, rev/min            (default 1800)
%     'DefectFreq'  Impact repetition (defect) freq, Hz     (default 118)
%     'Resonance'   Excited resonance frequency, Hz         (default 4500)
%     'Damping'     Damping ratio of the ringdown           (default 0.05)
%     'CarrierG'    Amplitude of 1X machine vibration, G    (default 0.30)
%     'ImpactG'     Peak amplitude of impact bursts, G      (default 0.80)
%     'NoiseG'      RMS of broadband noise, G               (default 0.04)
%     'Seed'        RNG seed for reproducibility            (default 1)
%
%   Outputs:
%     accel   Acceleration, G   (column vector)
%     t       Time, sec         (column vector)
%
%   Example:
%     [accel, t] = generate_bearing_signal('DefectFreq', 118, 'ShaftRPM', 1800);
%     plot(t, accel); xlabel('Time (sec)'); ylabel('Accel (G)');

    % ---- Parse inputs ---------------------------------------------------
    p = inputParser;
    addParameter(p, 'Fs',         50000);
    addParameter(p, 'Duration',   1.0);
    addParameter(p, 'ShaftRPM',   1800);
    addParameter(p, 'DefectFreq', 118);
    addParameter(p, 'Resonance',  4500);
    addParameter(p, 'Damping',    0.05);
    addParameter(p, 'CarrierG',   0.30);
    addParameter(p, 'ImpactG',    0.80);
    addParameter(p, 'NoiseG',     0.04);
    addParameter(p, 'Seed',       1);
    parse(p, varargin{:});
    o = p.Results;

    rng(o.Seed);

    % ---- Time base ------------------------------------------------------
    dt = 1 / o.Fs;
    t  = (0:dt:o.Duration - dt).';   % column vector, sec
    N  = numel(t);

    % ---- Low-frequency machine vibration (the visible carrier) ----------
    f1 = o.ShaftRPM / 60;            % 1X shaft speed, Hz (= 30 Hz here)
    carrier = o.CarrierG      * sin(2*pi*f1   * t) ...   % 1X
            + 0.35*o.CarrierG * sin(2*pi*2*f1 * t + 0.4); % 2X harmonic

    % ---- Periodic impact bursts (exponentially-decaying resonance ring) -
    wn    = 2*pi*o.Resonance;        % resonance, rad/s
    zeta  = o.Damping;
    wd    = wn * sqrt(1 - zeta^2);   % damped natural frequency

    Timpact = 1 / o.DefectFreq;      % impact spacing, sec
    impactTimes = 0 : Timpact : o.Duration;

    impacts = zeros(N, 1);
    for k = 1:numel(impactTimes)
        t0  = impactTimes(k);
        % small slip/jitter (~2%) so bursts are not perfectly periodic
        t0  = t0 + 0.02 * Timpact * (rand - 0.5);

        % amplitude modulation: bursts loudest near the loaded zone, so
        % vary peak height a bit to mimic the figure's uneven burst sizes
        Ak  = o.ImpactG * (0.7 + 0.6*rand);

        tau = t - t0;                % time since this impact
        mask = tau >= 0;             % causal: ring only after the strike
        ring = zeros(N, 1);
        ring(mask) = Ak .* exp(-zeta * wn * tau(mask)) ...
                        .* sin(wd * tau(mask));
        impacts = impacts + ring;
    end

    % ---- Broadband measurement noise ------------------------------------
    noise = o.NoiseG * randn(N, 1);

    % ---- Total raw acceleration -----------------------------------------
    accel = carrier + impacts + noise;
end