How to get a signal without NaN values?

I have a signal and I have applied the Shannon's Nyquist Theorem to get the rate and the reconstructed signal by following formulas.
But I am getting NaN values in the Rate Signal. Is there any way that to not get the NaN values? Below is my code.
close all; clear all; clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X=[11477.9
11417.3
11426.4
11413
11382.9
11375.1
11347.9
11351.1
11329.3
11318.9
11343.4
11360.7
11378.4
11414.2
11408.2
11413.6
11449.4
11444.1
11421.4
11388.5
11429.3
11438.1
11432.9
11408.2
11434.3
11444.8
11443.5
11491.8
11503.8
11581.8
11525.9
11529.5
11557.6
11523
11516
11504.1
11492.9
11480.3
11495.9
11445.1
11437.7
11468.5
11463
11488.5
11470.3
11491.1
11480.2
11488.9
11453.8
11456
11434.5
11460.6
11456.1
11454
11468.3
11464.4
11501
11453.8
11474.2
11458.2
11452.8
11434.6
11447.3
11379.5
11396.4
11409.9
11405.6
11433.3
11435.6
11434.2
11415.5
11483.6
11434.6
11452.2
11471.5
11445
11462.6
11433.3
11440.2
11442.3
11456
11408.6
11399.7
11369.8
11372.6
11401.4
11405
11426.6
11430.3
11405.6
11409.8
11405
11399.7
11381.2
11392.5
11383.6
11365.8
11362.4
11347.8
11324.9
11353
11330.8
11319.6
11313.2
11322.2
11294.4
11341
11345.3
11325.8
11359.3
11353.1
11330.7
11344.4
11361.9
11375.6
11338.2
11366.3
11349.5
11354.3
11373.5
11396.7
11387.6
11402
11415.8
11401.3
11416.9
11408.7
11371.5
11403.5
11437.4
11438.5
11452.5
11468.8
11428.4
11418.4
11395.3
11437.6
11409
11428.3
11375.6
11361.3
11343.6
11350.5
11357.6
11354.6
11394.5
11404.6
11449.5
11423
11451.8
11455.4
11472.9
11431.5
11453.7
11438.5
11433.6
11425.8
11413.5
11418.7
11395
11404.8
11367.5
11365.8
11351.3
11340.4
11374.3
11365
11397.8
11404
11387.6
11361.9
11354.5
11348.3
11297.2
11284.1
11308.6
11339.1
11354.3
11350.4
11350.6
11353.5
11353.7
11295.5
11303.3
11267.1
11333.3
11351.6
11330.9
11330.8
11336.9
11359.3
11361.6
11382.9
11339.5
11346
11403.5
11371.2
11381.8
11340.7
11374.2
11360.8
11329.8
11361.6
11346.2
11343.8
11313.9
11294.8
11277.7
11289.3
11295
11302.2
11329.8
11339.1
11352.2
11348.7
11353.5
11354.6
11356.4
11336.3
11327
11366.5
11362.4
11372.7
11367
11353.1
11390.2
11391.7
11400.2
11387.9
11371.3
11377.6
11390
11442.7
11429
11445.1
11449.3
11452
11385.5
11395.6
11387.1
11355.4
11334.4
11346.1
11317.3
11337.9
11330.4
11339.2
11351.5
11336.3
11362.8
11342.8
11357.4
11355.3
11364.1
11333.1
11345.1
11355
11349.8
11376.7
11367.7
11394.9
11421.2
11434.2
11459.5
11448.3
11446.3
11432.8
11441.2
11380.6
11395.9
11394.5
11380.9
11364.8
11336.5
11346.2
11376.7
11392.8
11330.1
11361.6
11334.2
11341.4
11327.7
11315.8
11344.8
11328.6
11341.5
11350.6
11363.7
11383.3
11375
11398
11381.4
11411.2
11426.3
11440
11432.5
11438
11436.1
11467.1
11506.6
11461.1
11493.6
11487.1
11461.8
11417.7
11429.3
11406.7
11400.5
11401
11387.3
11399.4
11395.1
11401.3
11379.5
11400.8
11408.7
11447.4
11460.4
11458.7
11432.9
11454.1
11480.8
11474.9
11504.1
11463.2
11530
11539
11531.9
11524.3
11484.2
11521.7
11490
11498.9
11451.1
11461.9
11486.6
11459.8
11463.9
11464.4
11513.1
11529
11534.2
11547.4
11564.6
11566.9
11485.5
11502.6
11471.7
11501.8
11466.6
11410.4
11426.5
11390.9
11426.5
11363.5
11366.3
11389.5
11368.3
11329.5
11315.5
11329.6
11310.7
11318.2
11272.7
11320.9
11320.9
11326.6
11351.1
11330.3
11391.4
11390.9
11388.4
11364.3
11354.7
11374.6
11355
11357.8
11332.7
11376.9
11348.4
11339.5
11382
11370
11343.6
11335.1
11321
11327.7
11294
11302.5
11302.6
11304.3
11299.9
11294.7
11298.9
11305.2
11341
11363.8
11384.5
11389.7
11371.8
11390.4
11402.2
11387.3
11387.6
11387.3
11394.4
11447.1
11465
11431.5
11457.1
11460.6
11485.7
11491
11458.5
11440.9
11434.8
11454.9
11453.5
11463
11446.3
11475.9
11482.5
11457.3
11454.8
11445
11442.8
11440.9
11386.9
11412.6
11387.1
11402
11376.8
11425.7
11426
11426.7
11421.5
11438.4
11455.1
11423
11450
11429.3
11486
11504.3
11494.1
11510.8
11527.5
11562.5
11570.8
11520.7
11496.2
11508.6
11514.3
11457.7
11457.8
11435.8
11465
11475.5
11459.7
11462.8
11452.2
11499.4
11508
11478.9
11483.9
11479.6
11493.1
11451.4
11421.4
11366.3
11389.8
11385
11339.1
11322.4
11336.5
11346.9
11315.2
11328.6
11323
11348.3
11351.3
11346.5
11369.6
11400.5
11426.4
11432.4
11404.5
11415
11428.9
11446.8
11408.2
11417.9
11379.6
11379.4
11411
11383.2
11409.2
11443.8
11453.7
11455.9
11420.4
11392.9
11395.4
11389.9
11358.4
11370.3
11404.4
11406
11418.9
11404.7
11460.1
11472.4
11510.2
11472
11457.3
11493.7
11507.3
11512.8
11489.1
11481.3
11481.6
11451.9
11423.8
11336.1
11413.5
11412.1
11376.1
11368.6
11371.8
11361.7
11320.5
11294.2
11268.4
11302.5
11344.5
11320.8
11345.4
11308.4
11327.4
11337.9
11340
11354.1
11355.1
11376.6
11406.4
11422.9
11434.4
11441.6
11457.7
11474.1
11487.9
11489.5
11461.2
11477.4
11464.9
11506.4
11484.7
11476.8
11466.2
11746
11060.5
11805
11714
11676.6
11674.9
11609.2
11516.9
11505
11534.9
11527.1
11514.4
11513.6
11535.7
11450
11435.1
11390.1
11426.6
11406
11385.3
11359.2
11332.5
11327.7
11302.2
11296.2
11267
11269.5
11246.4
11260.3
11284.6
11268.1
11257.5
11258.9
11252.8
11259.1
11304.2
11286.2
11319.8
11441.8
11558.1
11648.4
11671.6
11658.8
11612.9
11656.8
11823
11659.9
11559.5
11550
11601.1
11528.9
11489.8
11456.9
11447
11441.3
11456.1
11463.4
11443.9
11408.3
11414.5
11406.2
11408.4
11438.5
11427.3
11461.3
11444.7
11421.3
11413.1
11407.6
11421.8
11395
11379.2
11344.8
11333.3
11299
11296.6
11275.3
11275.4
11275.3
11268.8
11279.5
11304.4
11335.2
11284.6
11261.9
11256.3
11237.6
11242.8
11250.3
11298.5
11313.5
11262.5
11254.6
11256.5
11259.6
11258.3
11257.5
11269.6
11321.6
11369.7
11379.5
11356.1
11311.4
11309.1
11296.4
11322.4
11369.7
11398.9
11419.5
11399.9
11398.2
11425.6
11452.6
11476
11492
11438
11413.4
11441.6
11498.6
11481.6
11469.9
11448.4
11434.1
11452.9
11483.7
11463
11470.8
11447.8
11445.5
11446.8
11424.1
11438.2
11465.9
11454.2
11446.3
11448.6
11431.1
11428.8
11416.1
11407.1
11412.1
11448.8
11449.7
11442.1
11424.9
11409.3
11402.4
11386.8
11413.2
11434.9
11447.9
11441.4
11434.9
11405.2
11414.3
11416.5
11437.9
11455.2
11404.8
11422.6
11435.6
11424.7
11457.5
11439
11442.2
11398.3
11381
11424.2
11421.2
11391
11390.9
11384.8
11402
11396.7
11382.7
11388.9
11357.5
11377.2
11353.7
11334.6
11346.5
11335.4
11330.6
11321
11329.2
11354.6
11369.8
11361
11363
11355.9
11399.1
11394
11410.7
11394
11405.1
11407.4
11399.2
11438
11435.8
11429.7
11419.8
11387.2
11375.3
11381.1
11381.3
11397.8
11383.4
11364.6
11332.7
11317
11345.3
11322
11359.8
11337.2
11331.4
11319.2
11321.3
11325
11334.7
11360.3
11356
11337.7
11343.6
11353.7
11379.3
11436.6
11428.2
11437.8
11411.4
11438.8
11461.5
11468.5
11489.3
11459.3
11437.7
11415.6
11398.6
11418.8
11415.6
11394.3
11380
11365.9
11366.6
11369.4
11386.2
11405.7
11349
11372.3
11370.5
11370.2
11402.6
11385
11401.8
11382.5
11362.5
11353.6
11368.9
11365.3
11377.3
11375.3
11385.2
11364.7
11357
11366.8
11337.1
11348.1
11316.3
11308.3
11322.2
11311
11341.8
11350.1
11340.1
11348.3
11338.4
11379.9
11395.1
11368.1
11363.4
11342.3
11329.5
11317.9
11318.7
11304.5
11256.4
11277.6
11247.3
11264.2
11247.9
11245.6
11243.5
11257.1
11289.5
11250.5
11252.9
11243.2
11278.9
11323.5
11299.4
11328.1
11306.9
11311.5
11317.8
11308.3
11302.3
11276.2
11295.2
11252.7
11267.4
11268.3
11265.8
11271.8
11266.6
11280.2
11273.3
11300.6
11280.5
11298.7
11341.9
11336.2
11354.5
11351
11344.7
11346.5
11321.8
11358.6
11340.5
11352.6
11333.3
11347.1
11369.5
11352.9
11359.9
11335.2
11308.9
11331.4
11319.5
11344.5
11337.1
11352.8
11371.9
11359.4
11373.2
11339.4
11340.5
11343.4
11326.3
11332
11300.9
11335.2
11319.5
11327
11353.4
11358.1
11376.5
11356.3
11369.2
11381.7
11364.1
11392.4
11363.8
11376.8
11371.5
11366
11401
11366
11374.2
11347.6
11350.8
11354.1
11348.3
11377.9
11350.9
11384.9
11370.2
11378.9
11410.1
11399.8
11422.8
11390.8
11410.8
11377.9
11365.6
11390
11363
11373.6
11358.8
11350.5
11370.1
11377.4
11421.5
11392.7
11429.5
11429.2
11440.5
11469.2
11415.8
11402.9
11349.8
11338.3
11312.8
11319.9
11323.2
11316.9
11320.3
11304.7
11311
11360.6
11354.3
11396.2
11376.8
11390.6
11404.5
11413.7
11394.7
11377.3
11346.3
11358.1
11339.4
11379.6
11358.9
11372.3
11377.1
11367.6
11389.9
11360
11408.8
11376
11373.3
11344.2
11342.9
11355.1
11328.9
11348.1
11348.7
11350.8
11371.7
11362.2
11360.3
11338.7
11319.6
11306.7
11320.4
11350.3
11314.6
11356.8
11356.7
11398.8
11377.8
11368
11365.7
11357.6
11368.1
11362.3
11385.3
11380.3
11398.1
11368.4
11390.9
11410.7
11371.2
11386.2
11375.6
11370.2
11401.5
11394.5
11388.7
11370.1
11391.7
11399.6
11393.8
11398.6
11417.7
11413.5
11397
11432.9
11415.3
11423.2
11402.4
11399.1
11402.7
11390.5
11421.6
11378.3
11409.1
11408.5
11408.3
11442.2
11410.2
11449.1
11428.1
11444.5
11461.2
11437.1
11380
11384.3
11380.8
11356.6
11371.3
11360.7
11384.6
11389.7
11364.9
11370.7
11354
11356.8
11380.4
11371.1
11396.3
11370
11392.6
11369.9
11382.5
11372.3
11359.6
11335
11324.7
11337.8
11338.7
11345
11321.7
11350.4
11345.6
11366.6
11346
11325
11331.1
11336.8
11337.5
11323.5
11337.6
11310.2
11316.2
11367.4
11346.4
11372
11379.5
11389.3
11374.4
11372.5
11371.7
11356
11354.2
11335.5
11372.6
11370.7
11386.8
11363.4
11358.3
11397.9
11383.7
11396.8
11397.8
11385.2
11401.2
11395.9
11398.1
11394
11399.5
11373.6
11388.2
11377.5
11394.6
11414
11393.3
11406.4
11405.6
11410.5
11434.6
11413.3
11453.9
11439.2
11457.6
11458.4
11446.7
11439.1
11433.6
11441.8
11414.7
11436.4
11403.5
11450.9
11432.5
11439.6
11473.3
11449.6
11463.1
11452.3
11427.2
11401.4
11405.6
11360.7
11366.5
11336.1
11317.6
11289.2
11306.6
11306.7
11302.1
11350.6
11343.2
11368.7
11370
11365.4
11334.1
11317.6
11336
11305.5
11330.4
11306.5
11317.9
11321.9
11313.5
11344.5
11307.2
11335.5
11316.7
11334.7
11313.4
11326.3
11313.2
11307.2
11334.8
11321
11323.6
11317.9
11338.3
11330.7
11314.4
11334.7
11335.3
11353.3
11341.7
11366.5
11351.7
11353.6
11364.3
11361
11351.9
11337.9
11372.8
];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Addnum=20;%Number of additional data points in each time interval.
N=Addnum*length(X); %Length of the reconstructed signal(the continuous signal);
Rec=zeros([1,N]);
Rate=zeros([1,N]);
nx=zeros([1,N]);
NX=zeros([1,length(X)]);
zeroline=zeros([1,N]);
index=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:N
if mod(i,Addnum)==1
index=index+1;
NX(index)=i;
end
nx(i)=i;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:N
Rec(i)=0;
Rate(i)=0;
for j=1:length(X)
Rec(i)=Rec(i)+X(j)*sinc((1/Addnum)*(i-Addnum*(j-1)));
Rate(i)=Rate(i)+X(j)*(cos((pi/Addnum)*(i-Addnum*(j-1)))/(i-Addnum*(j-1))-sin((pi/Addnum)*(i-Addnum*(j-1)))/((pi/Addnum)*(i-Addnum*(j-1))^2));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mean_X=mean(X);
X=X-mean_X;
mean_Rec=mean(Rec);
Rec=Rec-mean_Rec;
plot(nx,Rec,'sg');
hold on
plot(nx,zeroline,'k');
plot(nx,Rate,'*b');
plot(NX,X,'or')
axis([1000 10000 -400 600])
legend ('Reconstructed Signal','zeroline','Rate Signal','BOLD Signal X')

4 Comments

Only variable Rate contains NaN values. Looking at the sine term in the formula of Rate
sin((pi/Addnum)*(i-Addnum*(j-1)))/((pi/Addnum)*(i-Addnum*(j-1))^2))
A common expression in numerator and denominator is
(i-Addnum*(j-1))
which becomes 0 when i=20, j=2, thus the term yields the form 0/0 which is NaN.
If you don't want to get NaN values, either you can remove the NaN values or modify your formula.
How to modify formula so I don't get NaN values?
What you are trying to do for the variable Rate? Do you have a reference formula?
What value it should be instead of NaN?
I have attached a formula for rate signal in the image. The value can be anything other than NaN

Sign in to comment.

 Accepted Answer

Try removing the nans or infs
badIndexes = isnan(Rate) | isinf(Rate);
nx2 = nx(~badIndexes);
Rate2 = Rate(~badIndexes);
plot(nx2, Rate2, '*b');

10 Comments

Haya Ali
Haya Ali on 12 Mar 2023
Edited: Haya Ali on 12 Mar 2023
Is there something that can be done without removing the NaNs? Co example changing the indexing or loop?
"without removing the NaNs? " is what you just asked, but you also said "The value can be anything other than NaN" so no, if you change it to another value, such as zero by doing
badIndexes = isnan(Rate) | isinf(Rate);
Rate(badIndexes) = 0; % Change NaNs to 0.
plot(nx2, Rate, '*b');
the NaNs will be gone because you replaced them with zeros..
Change
Rate(i)=Rate(i)+X(j)*(cos((pi/Addnum)*(i-Addnum*(j-1)))/(i-Addnum*(j-1))-sin((pi/Addnum)*(i-Addnum*(j-1)))/((pi/Addnum)*(i-Addnum*(j-1))^2));
to
denom1 = (i-Addnum*(j-1));
denom2 = ((pi/Addnum)*(i-Addnum*(j-1))^2);
if denom1 ~= 0 && denom2 ~= 0
Rate(i) = Rate(i) + X(j)*(cos((pi/Addnum)*(i-Addnum*(j-1)))/denom1-sin((pi/Addnum)*(i-Addnum*(j-1)))/denom2);
end
%nothing to do in false case, we skip the 0 / 0
This way the division by 0 never happens and so the nan never gets generated, but you would still have achieved your goal of not removing the nans (because the nans would not be generated at all.)
Thanks a lot Walter!
You can only check for denom1, as denom2 will always be zero when denom1 is zero.
How? Please tell me through code.
@Walter Roberson did that. I know you saw the code where he checked because you said "Thanks a lot Walter!" If you want you can do
denom1 = i - Addnum*(j-1);
denom2 = (pi/Addnum) * denom1 ^2);
if denom1 ~= 0
Rate(i) = Rate(i) + X(j) * (cos((pi/Addnum)*(i-Addnum*(j-1))) / denom1-sin((pi/Addnum)*(i-Addnum*(j-1)))/denom2);
end
%nothing to do in false case, we skip the 0 / 0
Yeah he thankfully did that. I though you are trying any other method. Thanks for your suggestions too.
OK. Though it's probably more trouble to avoid/prevent the NaNs than to just fix them afterwards. But whatever you intuitively feel most comfortable with is fine.
The code involves loop summing terms, some of which might be Nan. If you detect the potential for NaN at the term level, then you can omit adding that term to the summation, so you end up with a sum of non-NaN values that might contain useful information. If you wait until afterwards to detect the NaN then you lose the information in the non-NaN terms.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!