- 积分
- 590
- 回帖
- 0
- 西莫币
-
- 贡献
-
- 威望
-
- 存款
-
- 阅读权限
- 20
- 最后登录
- 1970-1-1
该用户从未签到
|
发表于 2011-4-22 10:27
|
显示全部楼层
来自: 中国湖北武汉
附件添加没成功
直接把程序粘贴如下:
clc
clear all;
format long;
Ns=936;
order=11;
%**********************read the position and flux density************************
fid=fopen('456.dat','r'); %open the original file
%'r'open file for reading
fidnew = fopen('b1.dat','w'); %creating and write the new file ,
%'w'open file for writing; discard existing contents
while feof(fid)==0 %feof=1 indecate the end of the file
tline = fgetl(fid); %read a line from the file
if ~ischar(tline),% "ischar" to see if tline is a character or not,is 1,no 0
break,
end
temp=abs(tline); %absolute value of tline,put tline value into temps
Nlength=length(tline); % tline's length
isemptyline=0; %new variable "isemptyline"and it is 0
if Nlength==0 %check if the line is empty
isemptyline=1; %is an empty line,then isempty=1
end
allspace=0; %two new variables
isspace=0; %
%%%%%% a loop to check if the line is all spaces %%%%%%%%%%%%%%
for i=1:Nlength % a loop to check if the line is all spaces
T=temp(i);
if T==32 %
isspace=isspace+1;
end
if isspace==Nlength
allspace=1;
break
end
end
%%%%%-------------------------------------------%%%%%%%%
findalpha=0; %new variable,find english characters in the line
for j=1:Nlength
T=temp(j);
if ((T>=65)&(T>=90))|((T>=97)&(T>=122))
findalpha=1;
break;
end
end
%%%------if the line with true char,not all space,not empty line----%%%%%
if (~findalpha)&(~allspace)&(isemptyline==0) % three condition all true
fprintf(fidnew,tline);% write the tline to the new file B.dat
fprintf(fidnew,'\n'); % after write the line finish it with a "\n"
end
end
%%-------after read the file ,close the file-----%%
fclose(fid);
fclose(fidnew);
%%%--------------------------------------%%%
fid1=fopen('b1.dat','r');% new virable fid1,which store the data.
flux_position =fscanf(fid1,'%f',[2,Ns]);% [M,N] read at most M * N elements filling at least an
% M-by-N matrix, in column order. N
% can be inf, but not M.
% %f means output/input a float
% data
fclose(fid1); %close fid1
%********************************read file finish*****************************************
flux_position=flux_position';%turn the flux_position into a column matrix
pos=flux_position(:,1);% pos equals to the first line of tline
flux=flux_position(:,2);% flux equals to the secend line of tline
figure;% plot new figure
plot(pos,flux,'r');%plot origional waveform
hold on;
grid on;
fft1=fft(flux,Ns);% do the fft convert to the flux with the number of flux
j=0; % new variable
amp_har=zeros(1,(order+1)/2);% ZEROS(M,N) or ZEROS([M,N])
% is an M-by-N matrix of zeros.order=11
% build new zero matrix
for m=1:2:order % m=1,3,5,7,9,11.....
j=j+1;
fft1=fft(flux,Ns); % details refers to 'help fft'
fund_ele_front=fft1(m+1); %if Ns=900,m=1 here is the 2th number
fund_ele_back=fft1(Ns+1-m); % here is the 899th number
amp_har(j)=(abs(fund_ele_front))/Ns*2; %amplitude of the Harmonicm,(here is the point)
fft1=0*fft1; %turn the fft1 into 0
fft1(m+1)=fund_ele_front;
fft1(Ns+1-m)=fund_ele_back;
fft1=ifft(fft1,Ns); % IFFT(X,N) is the N-point inverse discrete Fourier transform
fft1=real(fft1); % REAL Complex real part
if m==1
plot(pos,fft1,'b');
X1=pos;
Y1=fft1;
%figure;
%plot(X,Y,'m');
%grid on;
else
plot(pos,fft1,'b');
end
hold on;
end
k=(1:2:order);
figure;
bar(k,amp_har,0.5);
grid on;
%peak_b=max(fft1)
%rms_b=0.707*peak_b |
评分
-
查看全部评分
|