이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Query regarding calculating frequency and amplitude
조회 수: 1 (최근 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193745/image.png)
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
참고 항목
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 (한국어)