Outlining values of Stem FFT plot to create a trace profile.

I have converted accelerometer data into the frequency domain using a FFT in order to look for unique characteristics between different classes. I am trying to create an outline of FFT stem plot data for a computationally smoothed line rather than hand drawn lines for visual analysis. Any thoughts you have is greatly appreciated, thank you. Is it better to smooth and then plot over the stem or to filter the data?
I've included my code to show the frequency domain stem plot comparisons as well as a png of what I am trying to figure out with matlab.
load StavrosLeft.txt
load StavrosRight.txt
%% Stavros
figure
sgtitle('Stavros Accelerometer Frequency Domain')
sampfreq=62.5; %% sampling frequency
%Left Accelerometer
lengthStavrosLAY=length(StavrosLeft(:,4)); %length of force data for accelerometer in Y
FDStavrosLAY=(2/lengthStavrosLAY)*(fft(StavrosLeft(:,4))); %Transformation from time domain to frequency domain of accel data
faStavrosLAY=linspace(-sampfreq,sampfreq,lengthStavrosLAY); %creation of linearly spaced vector for the length of the domain between the frequency
subplot(2,1,1)
stem(faStavrosLAY, abs(FDStavrosLAY)) %plotting the FFT on the linspace
ylim([0,1])
ylabel('Amplitude Left')
xlabel('Frequency')
title('Y')
hold on
%Right Accelerometer
lengthSandRAY=length(StavrosRight(:,4));
FDStavrosRAY=(2/lengthSandRAY)*(fft(StavrosRight(:,4)));
faStavrosRAY=linspace(-sampfreq,sampfreq,lengthSandRAY);
subplot(2,1,2)
stem(faStavrosRAY, abs(FDStavrosRAY))
ylim([0,1])
ylabel('Amplitude Right')
xlabel('Frequency')
title('X')

 Réponse acceptée

hello
FYI, this is a linear averaged FFT spectrum (one sidded) + enveloppe in red
hope its helps
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
signal = load('StavrosLeft.txt');
[samples,channels] = size(signal);
selected_channel = 4; % select your data channel here
signal = signal(:,selected_channel);
Fs = 62.5; % to be checked
dt = 1/Fs;
time = (0:samples-1)*dt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display 1 : averaged FFT spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NFFT = 1024; % fft length
OVERLAP = 0.75; % between 0 and 1 max ; buffer overlap = OVERLAP*NFFT
[freq, sensor_spectrum] = myfft_peak(signal,Fs,NFFT,OVERLAP);
% add upper enveloppe
[YUPPER,YLOWER] = envelope(sensor_spectrum,50,'peak');
figure(1),plot(freq,sensor_spectrum,'b',freq,YUPPER,'r');grid on
title(['Averaged FFT Spectrum / Fs = ' num2str(0.1*round(10*Fs)) ' Hz / Delta f = ' num2str(0.1*round(10*(freq(2)-freq(1)))) ' Hz ']);
xlabel('Frequency (Hz)');ylabel('Amplitude');
function [freq_vector,fft_spectrum] = myfft_peak(signal, Fs, nfft, Overlap)
% FFT peak spectrum of signal (example sinus amplitude 1 = 0 dB after fft).
% Linear averaging
% signal - input signal,
% Fs - Sampling frequency (Hz).
% nfft - FFT window size
% Overlap - buffer percentage of overlap % (between 0 and 0.95)
[samples,channels] = size(signal);
% fill signal with zeros if its length is lower than nfft
if samples<nfft
s_tmp = zeros(nfft,channels);
s_tmp((1:samples),:) = signal;
signal = s_tmp;
samples = nfft;
end
% window : hanning
window = hanning(nfft);
window = window(:);
% compute fft with overlap
offset = fix((1-Overlap)*nfft);
spectnum = 1+ fix((samples-nfft)/offset); % Number of windows
% % for info is equivalent to :
% noverlap = Overlap*nfft;
% spectnum = fix((samples-noverlap)/(nfft-noverlap)); % Number of windows
% main loop
fft_spectrum = 0;
for i=1:spectnum
start = (i-1)*offset;
sw = signal((1+start):(start+nfft),:).*(window*ones(1,channels));
fft_spectrum = fft_spectrum + (abs(fft(sw))*4/nfft); % X=fft(x.*hanning(N))*4/N; % hanning only
end
fft_spectrum = fft_spectrum/spectnum; % to do linear averaging scaling
% one sidded fft spectrum % Select first half
if rem(nfft,2) % nfft odd
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
fft_spectrum = fft_spectrum(select,:);
freq_vector = (select - 1)*Fs/nfft;
end

3 commentaires

This is great, thank you for your help. Is there a way too overlay it on the original graph or continue it for the right side as well?
hello
so you wantt o keep the two sidded FFT ? the negative frequency data is only the mirrored version of the positive frequency data
You can also use the envelope technique on your fft data
all the best
Taylor Knuth
Taylor Knuth le 1 Nov 2021
Modifié(e) : Taylor Knuth le 1 Nov 2021
Thanks,I think this will work. I'll have to dive down through the code to fully understand it.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by