現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)