dicklyon@619: function layer_struct = SAI_StabilizeLayer(layer_struct) dicklyon@619: % Pick trigger points in buffer, shift rows to offset_from_end, dicklyon@619: % and blend into frame dicklyon@619: dicklyon@619: frame = layer_struct.frame; dicklyon@619: dicklyon@619: nap_buffer = real(layer_struct.nap_buffer); dicklyon@619: n_buffer_times = size(nap_buffer, 1); dicklyon@619: [n_ch, width] = size(frame); dicklyon@619: dicklyon@619: % Make the window to use for all the channels at this layer. ronw@675: window_width = layer_struct.window_width; dicklyon@619: n_window_pos = layer_struct.n_window_pos; dicklyon@619: % Windows are always (approx) 50% overlapped: ronw@675: window_hop = window_width / 2; dicklyon@619: ronw@675: window = sin((1:window_width)' * pi / window_width); ronw@675: window_start = (n_buffer_times - window_width) - ... ronw@675: floor((n_window_pos - 1) * window_hop); ronw@675: window_range = (1:window_width) + window_start - layer_struct.future_lags; dicklyon@619: % This should not go negative! ronw@675: offset_range = (1:width) + window_start - width; dicklyon@619: % CHECK dicklyon@619: if any(offset_range < 0) dicklyon@619: error; dicklyon@619: end dicklyon@619: dicklyon@619: % smooth across channels; more in later layers dicklyon@619: smoothed_buffer = smooth1d(nap_buffer', layer_struct.channel_smoothing_scale)'; dicklyon@619: dicklyon@619: % For each buffer column (channel), pick a trigger and align into SAI_frame dicklyon@619: for ch = 1:n_ch dicklyon@619: smooth_wave = smoothed_buffer(:, ch); % for the trigger dicklyon@619: dicklyon@619: % Do several window positions and triggers dicklyon@619: for w = 1:n_window_pos ronw@675: % move the window to later and go again ronw@675: current_window_offset = floor((w - 1) * window_hop); ronw@675: [peak_val, trigger_time] = ... ronw@675: max(smooth_wave(window_range + current_window_offset) .* window); dicklyon@619: nap_wave = nap_buffer(:, ch); % for the waveform dicklyon@619: if peak_val <= 0 % just use window center instead dicklyon@619: [peak_val, trigger_time] = max(window); dicklyon@619: end ronw@675: trigger_time = trigger_time + current_window_offset; dicklyon@619: alpha = (0.025 + peak_val) / (0.5 + peak_val); % alpha 0.05 to near 1.0 dicklyon@619: frame(ch, :) = alpha * nap_wave(trigger_time + offset_range)' + ... dicklyon@619: (1 - alpha) * frame(ch, :); dicklyon@619: end dicklyon@619: end dicklyon@619: dicklyon@619: layer_struct.frame = frame;