Why do i keep getting the ereor "The value assigned to variable *** might be unused"

2 views (last 30 days)
Karim Aawar
Karim Aawar on 2 Jul 2022
Edited: dpb on 2 Jul 2022
function strain_value= acquirestrain(uncomp_window,comp_window,fs)
NFFT = length(rcv_uncomp); %Length of the RF signal
%% PSD of Uncompressed Signal
z_uncomp=fft(uncomp_window);
PSD_uncomp= (abs(z_uncomp).^2)/(NFFT*fs); %Transforming the FFT to PSD by multiplying with the length and sample frequency
%Single Sided Spectrum
PSD__uncomp_single= PSD_uncomp(NFFT/2+1:end); %Get the real half of the signal (ERROR occurs here)
PSD_uncomp_single(2:end-1)=2*PSD_uncomp_single(2:end-1); %And double it to account for the imaginary half (ERROR occurs here)
%% PSD of Compressed Signal
z_comp=fft(comp_window);
PSD_comp= (abs(z_comp).^2)/(NFFT*fs);
%Single Sided Spectrum
PSD_comp_single= PSD_comp(NFFT/2+1:end);
PSD_comp_single(2:end-1)=2*PSD_comp_single(2:end-1);
% Frequecy parameters
frequency_axis= fs*(0:(pizo_elements/2)-2)/pizo_elements;
fc1= find(PSD_uncomp,max(PSD_uncomp));
fc2= find(PSD_comp,max(PSD_comp));
fc1=frequency_axis(fc1);
fc2=frequency_axis(fc2);
strain_value= (fc2-fc1)/fc1;
end

Accepted Answer

dpb
dpb on 2 Jul 2022
The warning is given because your code never references/uses the frequency vector, f, after defining it -- there's nothing in the remaining code that makes any use of it; you could just as well not include the line that computes it.
Most of the time when someone does compute a PSD it is desired to plot it or determine the actual frequency of a spectral feature so the frequency vector values would be needed -- unless you do something more in your case, it actually isn't needed by the code as is.
We don't know what frequencyaxis is in the above code...
  3 Comments
dpb
dpb on 2 Jul 2022
...
PSD_comp_single= PSD_comp(NFFT/2+1:end);
PSD_comp_single(2:end-1)=2*PSD_comp_single(2:end-1);
% Frequecy parameters
frequency_axis= fs*(0:(pizo_elements/2)-2)/pizo_elements;
fc1= find(PSD_uncomp,max(PSD_uncomp));
fc2= find(PSD_comp,max(PSD_comp));
...
Same thing -- you don't use PSD_comp_single after you compute it; only the two other PSD_xxx variables.
I was coming back to amplify my earlier response on that point -
Just as a coding stylistic note; I think I would have kept the variable f you had originally and changed the reference or frequency_axis to f instead; it makes more sense to me that way and f for the PSD frequency is probably the most common nomenclature used.
So, I'd've probably written something like
f = fs*(0:(pizo_elements/2)-2)/pizo_elements;
[mxP_un,ic1]=max(PSD_uncomp_single);
[mxP_c,ic2]=max(PSD_comp_single);
fc1= f(ic1);
fc2= f(ic2);
strain_value= (fc2-fc1)/fc1;
letting max return the optional output of the index instead of using find and using the single-sided spectra to match up with the f vector

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!