Query regarding calculating frequency and amplitude
3 次查看(过去 30 天)
显示 更早的评论
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');
assert(fid>=3,msg)
C = textscan(fid, '%f%f', 'CommentStyle','#', 'CollectOutput',true);
fclose(fid);
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.
回答(1 个)
Eduard Reitmann
2018-8-3
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
%%Plot
figure;
subplot(2,1,1)
plot(t,X)
subplot(2,1,2)
plot(f,mag)
19 个评论
Akshat Shrivastava
2018-8-6
Hello, Thank you for the help! I have imported the text file and converted it into a table using 'import' option and then created a script file. After that i tried to trim the data for 5 seconds as suggested by you, but something is wrong. The code is below, could you please have a look and help me. Thanks agaian
%% Create output variable
Emma = table;
Emma.VarName1 = cell2mat(raw(:, 1));
Emma.VarName2 = cell2mat(raw(:, 2));
Emma.VarName3 = cell2mat(raw(:, 3));
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp R;
plot (Emma.VarName1, Emma.VarName2)
time = Emma.VarName1; value = Emma.VarName2;
indexOfComments = find(~isnan(Emma.VarName3));
timeofcomment = Emma.VarName1(indexOfComments); %for i = 1:length(time)
% index = find(Emma.VarName1 > timeofcomment(i) - 5 & Emma.VarName1 < timeofcomment(i) + 5);
%newTime = Emma.VarName1(index);
%newValue = Emma.VarName2(index)
t = Emma(:,1);
X = Emma(:,2);
% Trim data
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
% Frequency
f=(Fn*linspace(0,1,numel(mag)))'; % Output frequency
%% Plot figure;
subplot(2,1,1)
plot(t,X)
subplot(2,1,2)
plot(f,mag)
Eduard Reitmann
2018-8-6
Put this section after you have imported Emma:
t = Emma.VarName1;
X = Emma.VarName2;
indexOfComments = find(~isnan(Emma.VarName3),1,'first');
t_end = t(indexOfComments);
indexTrim = t >= t_end-5 & t <= t_end;
t = t(indexTrim);
X = X(indexTrim);
Fs = 1/(t(2)-t(1));
Akshat Shrivastava
2018-8-6
indexTrim = t >= t_end-5 & t <= t_end;
in this line, you have asked to consider the last time index for 5 seconds? I am getting plot something like this (file attached)
Eduard Reitmann
2018-8-6
Can you kindly send my a screenshot of?
t = Emma.VarName1;
X = Emma.VarName2;
plot(t,X);
I suspect the data is incorrectly imported. Also, please send me your script from top to bottom. Thanks.
Akshat Shrivastava
2018-8-6
Hello, I have attached both the plots plot (t,X) and (plot f,mag). I guess data is correctly imported for plot (t,X). I wish to calculate frequency and amplitude for plus and minus 5 seconds at every index of comment. I have attached the script file also. Please tell me if i am going right. Thanks
Eduard Reitmann
2018-8-6
The
indexTrim = t >= t_end-5 & t <= t_end;
line should be changed to:
indexTrim = t >= t_end - 5 & t <= t_end + 5;
It basically, finds the indices of all the values larger than t_end-5 and smaller than t_end+5. Thus, a 10 s band around t_end.
With regards to the plots that you sent, the top graph of "plot(f,mag).PNG" does not make sense because the x-axis is from 0 to 5. What is your t_end value? Also, what are the dimensions ("size(t)" and "size(X)") of your t and X variables? There are multiple lines in the top graphs of "plot(f,mag).PNG" which also doesn't make sense.
The "plot(t,X).PNG" graph seems believable.
Can you attach the data file?
Akshat Shrivastava
2018-8-7
Hello, 't_end value' are basically the 3rd column values which have comments on the data file which i have attached below. I wish to calculate frequency for a period of 10 seconds (5 sec before & 5 sec after) at each time there is a comment on the third column.
Eduard Reitmann
2018-8-7
One problem with the input data was that the time vector restarted at zero a couple of times.
Also, I did not realize that there were multiple events. The updated script (attached) should do the trick.
Akshat Shrivastava
2018-8-7
Yes, the problem with the input file was that. I was thinking if we have a text file which does not restart from 0 in between, in that case should I use the same code? I have attached the text file.
Akshat Shrivastava
2018-8-7
I tried to load 'sample.txt' file which does not restart from 0 in between. After plotting, I am getting Amplitude and frequency subplot. But the problem is that time on x-axis is till 0-5sec on first subplot and 24-36 sec on second subplot. But my data file is till 111.225 seconds. So i guess the data is incomplete.
Akshat Shrivastava
2018-8-7
Which means it is just taking the 10 sec time period only for the first 2 comments. and not after that.
Akshat Shrivastava
2018-8-8
@ Eduard Reitmann. : Can you please also tell me how to calculate mean frequency for the above data? Thank you
Eduard Reitmann
2018-8-8
The new 'sample.txt' file to 111.225 still restarted at some point.
Use the attached script with your original data file. The time vector is not important if you are only interested in frequency and the samples are evenly spaced. You only need the sample frequency (Fs). This updated script takes +-5s (10s) band around all the comment positions (assuming it is one continuous data file) and averages all the frequency responses.
Eduard Reitmann
2018-8-8
FYI the positions of the comments might need to be scrutinized. They overlap sometimes and are also at positions where the file seems to have erroneous data points.
Akshat Shrivastava
2018-8-8
Thank you for the help!! One last question i have. If i need to find only mean frequency in a way such as i calculate 5seconds mean freq. before the first comment and then for next 5 sec + next 5sec (if the second comment comes before the next 5 seconds, we claculate the mean freq for the remaining time and not full for the 5 seconds). After the second comment we again calculate the mean freq for next 5 sec + 5 sec. I have attached a graphical representation explanation of the above question as it may be difficult to understand.
Eduard Reitmann
2018-8-8
I would not recommend doing this automatically - it can be done but the effort outweighs the reward. Rather manually define all the sections using the attached script. Just populate "tpos" with all the sections you want, according to the newly defined time vector. Use first plot as reference.
Akshat Shrivastava
2018-8-8
I am sorry, I did not understand.How should i do it manually and how to populate "tpos" with all the sections.
Akshat Shrivastava
2018-8-9
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
2018-8-10
@ Eduard Reitmann: Kindly help me to find the mean frequency for all the 5 sec windows. Thank you
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 View and Analyze Simulation Results 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)