MATLAB Answers

Query regarding calculating frequency and amplitude

4 views (last 30 days)
Akshat Shrivastava
Akshat Shrivastava on 3 Aug 2018
Hello everyone, I am loading a text file which has has a big data (comprising of 2 columns and multiple rows). Data represents (Time and Electrical activity). So far, I have used this syntax to plot the text file :
[fid,msg] = fopen('sample.txt','rt');
C = textscan(fid, '%f%f', 'CommentStyle','#', 'CollectOutput',true);
M = C{1};
plot (M)
I am adding a screenshot of the plot as well as the text file. My next goal is to calculate the frequency and amplitude for a period of 5 seconds before t= 376.6 second (time has a comment in the text file.) Can someone please help me out with this. #Thanks in advance.


Sign in to comment.

Answers (1)

Eduard Reitmann
Eduard Reitmann on 3 Aug 2018
Hope this helps.
%%Create sample data with 5 Hz sinosoid (Do not include this section in
% your code)
n = 1001;
t = linspace(360,380,n)';
M = [t sin(5*2*pi*t)];
%%Reallocate data
t = M(:,1);
X = M(:,2);
% Trim data (5 seconds before 376.6s)
i5 = t >= 376.6-5 & t <= 376.6;
t = t(i5);
X = X(i5);
Fs = 1/(t(2)-t(1));
%%Calculate Fast Fourier Transform
n = numel(X);
Fn=Fs/2; % Nyquist frequency
Y=fft(X,n); % Calculate fft
fftUnique=Y(1:floor(n/2)); % Find unique values (Symetry)
% Magnitude
fftScale=fftUnique/n; % Scale fft
fftComp=fftScale*2; % Compensate for unique values
mag=abs(fftComp); % Calculate magnitude
% Phase (optional)
phaUnique=angle(fftUnique); % Calculate phase
pha=unwrap(phaUnique); % Correct phase angles to produce
% smoother phase plots
% Frequency
f=(Fn*linspace(0,1,numel(mag)))'; % Output frequency


Akshat Shrivastava
Akshat Shrivastava on 8 Aug 2018
I am sorry, I did not understand.How should i do it manually and how to populate "tpos" with all the sections.
Akshat Shrivastava
Akshat Shrivastava on 9 Aug 2018
Okay, I understood what you mean by doing it manually, but can you tell me how to calculate mean frequency for each these 5 seconds window?
Akshat Shrivastava
Akshat Shrivastava on 10 Aug 2018
@ Eduard Reitmann: Kindly help me to find the mean frequency for all the 5 sec windows. Thank you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!