使用八度音程将数据保存在for循环中

时间:2013-06-23 20:36:21

标签: loops save octave

我有一个八度的小问题。

我想模拟某些东西,因此我需要一个循环但不幸的是我的数据不会被八度音阶保存。我尝试了解决这个问题的一些可能性,但我找不到任何解决方案。

这是我的文件的代码:

%------ input from the user --------
%-----------------------------------

at=1;                  % acq. time in s

nu=300;               % Resonance frequency in Hz

scantime=1.1;          % time for a scan, >at in s

T1=(0.1*at:at/10:120*at)';                 % in s

T2=T1;                      % in s

noisestd=1;                 % noise std

expt = (at+scantime);                % experiment time



%-------RATIOS----------


T2T1ratio=(0.01:0.01:12)';                        %T2T1 ratio


att1ratio=at*1./(T1);                                %acquisition time by T1


%----------CALL CALCSPEC.M--------------
%----------------------------------------



for T1idx = (1:(rows(T1)))'
    for T2T1ratioidx = (1:rows(T2T1ratio))'
        for att1ratioidx = (1:rows(att1ratio))'
            spectrum = [s2n, peakwidth] = calcspec(at,nu,scantime,T1,T2,noisestd,expt);
            m={T1idx,T2T1ratioidx,att1ratioidx,s2n,peakwidth}
        endfor
    endfor
endfor

这是我的.m文件的代码:

function [s2n, peakwidth] = calcspec(at,nu,scantime,T1,T2,noisestd,expt);



%---- calculating input vars -----------------
%---------------------------------------------

ns=round(expt/scantime);                % nr. of scans

Fs = 4*nu;                              % Sampling frequency (>=2*nu by Nyquist)

t = ((0:(at*Fs-1))/Fs)';                   % Time vector

fn=2^nextpow2(at*Fs);                   % number of points for the FT (must be power of 2)

res = Fs/fn;                            % spectral resolution (bins in the fft)

freq=0:res:(Fs-res);                    % frequency axis

fid=exp(-i*2*pi*nu.*t).*exp(-t./T2);    % generation of "clean" FID

fid=ns*fid'/max(fid);                   % normalization of FID 

for scan=1:ns
    noisyfid = fid+=noisestd*randn(1,res+1)';       % noisy fid
endfor




% FOURIER TRANSFORM 
% -----------------

sig = fft(fid, fn);                                                   % FT of clean spec

noisysig = fft(noisyfid, fn);                                        % Fourier Transform of noisy spec


%-------Calc S2N ----------
%---------------------

s2n = var(real(sig))/var(real(noisysig));


% DETERMINE FULL WIDTH AT HALF MAXIMUM + INTENSITY
%----------------------------------------------------

peakwidth = fwhm(real(sig));

intensity = max(real(sig));

正如你所看到的那样(希望如此)我试图将数据保存在单元阵列中,但在计算之后只保存了s2n和peakwidth的一个值。

有人可以告诉我我做错了吗?

3 个答案:

答案 0 :(得分:0)

虽然您正在循环,但您在循环内的计算中根本没有使用索引。您可能需要做类似的事情:

[s2n(%appropriate index%), peakwidth(%appropriate index%)] = calcspec(at,nu,scantime,T1(T2T1ratioidx),T2(T2T1ratioidx),noisestd,expt);
% same applies for m calculation

此外,您在同一行上有两个=符号,不确定最终结果是什么,但可能不符合您的预期:

spectrum = [s2n, peakwidth] = ...

答案 1 :(得分:0)

很多你的答案!

我做的两个=标志,因为否则我的八度音乐给我的信息是有一些变量是未定义的,所以我尝试了这个。

现在我试图改变一切错误的但我仍然不理解指数的那个东西。现在我尝试在我的代码中使用索引T1idx,以便我现在得到:

    for T1idx = (1:(rows(T1)))
    for T2T1ratioidx = (1:rows(T2T1ratio))
        for att1ratioidx = (1:rows(att1ratio))
            [sig(T1idx),noisysig(T1idx)] = calcspec(at,nu,scantime,T1,T2,noisestd,expt);
            s2n = sig/noisysig;
            peakwidth = fwhm(real(sig));
            m={s2n,peakwidth};
        endfor
    endfor
endfor

但是现在我收到了消息:A(I) = X: X must have the same size as I并且我没有任何好主意来获得“更好”的指数......

我最后想得到一个具有以下结构的单元格数组:

T1idx   T2T1ratioidx   att1ratioidx       s2n         peakwidth
1          1                1          a value         a value
2          .                .              .              .
3          .                .              .              .
4
.

很清楚我的意思是什么?

答案 2 :(得分:0)

我认为之前一切都很好,除了换位。要保存你可能做的所有事情,比如

for T1idx = (1:(rows(T1)))
    for T2T1ratioidx = (1:rows(T2T1ratio))
        for att1ratioidx = (1:rows(att1ratio))
            spectrum = [s2n, peakwidth] = calcspec(at,nu,scantime,T1,T2,noisestd,expt);
            m=[m; {T1idx,T2T1ratioidx,att1ratioidx,s2n,peakwidth}]
        endfor
    endfor
endfor

编辑:让m看起来像你想要的那样:

i = 1;
m = [];
for T1idx = (1:(rows(T1)))
    for T2T1ratioidx = (1:rows(T2T1ratio))
        for att1ratioidx = (1:rows(att1ratio))
            spectrum = [s2n, peakwidth] = calcspec(at,nu,scantime,T1,T2,noisestd,expt);
            m(i,:)=[T1idx,T2T1ratioidx,att1ratioidx,s2n,peakwidth];
            i = i + 1;
        endfor
    endfor
endfor
m