Function Ouput Array With Repeated Elemnts

Hello everyone, i have the following function :
function [ph]=Inst_Phase_Extraction( Filt_Signal )
ph=zeros(size(Filt_Signal,1),size(Filt_Signal,2));
for i = 1:size(Filt_Signal,1)
%Hilbert Transform
hil=zeros(1,size(Filt_Signal,2));
hil(1,:)=hilbert(Filt_Signal(i,:));
ph(i,:)=atan2(imag(hil(1,:)), real(hil(1,:)));
end
end
The ouput i get (ph) has the 1st row repeated size(Filt_Signal,1) times...

7 Comments

There's a lot of unnecessary indexing in your code.
function [ph]=Inst_Phase_Extraction( Filt_Signal )
ph = zeros(size(Filt_Signal)); %I'am assuming Filt_Signal is 2d
for row = 1:size(Filt_Signal, 1)
hil = hilbert(Filt_Signal(row, :)); %no need to preallocate hil
ph(row, :) = atan2(imag(hil), real(hil)); %hil(1, :) is the same as hil
end
end
If that code produces the same values for all rows, then that's because the hilbert transform or atan2 produces the same result for the rows of your input. Considering that the documentation of hilbert states that The imaginary part is a version of the original real sequence with a 90° phase shift, I wouldn't be surprised that atan2 produces the same result for all inputs. But I don't know anything about hilbert transforms.
Unfortunately i dont think this is the case because if i calculate ph with the following code, outside the function, i get different results (not the first row repeated)
for row = 1:size(Filt_Signal, 1)
hil = hilbert(Filt_Signal(row, :));
ph(row, :) = atan2(imag(hil), real(hil));
end
Yes Filt_Signal is 2d
Right, then show us the inputs and the exact way you call the function with these inputs, so we can see what the problem is ourselves.
load('signal.mat')
%% Signal Filtering
sf=input('Define Sampling Frequency (Hz) : sf = '); %Sampling Frequency
filter_order=input('Define Filter Order : ' );
Freq_bounds=input('Choose Frequency Bounds (use brackets i.e [fmin fmax]) : ');
Filt_Signal=Filter(signal,Freq_bounds(1,1),Freq_bounds(1,2),sf,filter_order );
ph=Inst_Phase_Extraction(Filt_Signal);
where function Filter is :
[Filt_Signal]=Filter(signal,lower_limit,upper_limit,sf,filter_order )
Nyquist_freq = sf/2; % Use Of Nyquist Frequency In Order To Ensure The Validity of Shannon's Theory
lowcut = lower_limit/Nyquist_freq;
highcut = upper_limit/Nyquist_freq;
passband = [lowcut highcut]; % [fmin fmax]
[Bc Ac] = butter(filter_order, passband); % Creation Of Butterworth Filter
Filt_Signal=zeros(size(signal,1),size(signal,2));
for i=1:size(signal,1)
Filt_Signal(i,:) = filtfilt(Bc, Ac, signal(i,:));
end
end
Hope it helps
i just noticed that with following code in the main program produces the right results:
ph=zeros(size(Filt_Signal,1),size(Filt_Signal,2));
for i=1:size(Filt_Signal,1)
ph(i,:)=Inst_Phase_Extraction(Filt_Signal(i,:));
end
where :
function [ph]=Inst_Phase_Extraction( Filt_Signal )
hil=hilbert(Filt_Signal(1i,:));
ph=atan2(imag(hil), real(hil));
end
But i would prefer the for loop to be in the function as i mentioned in the first question
Please, remove all these extra blank lines you've added, then select all the code and press the {}Code button with the code selected.
This will make your posts a lot more readable.
load('signal.mat')
%%Signal Filtering
sf=input('Define Sampling Frequency (Hz) : sf = '); %Sampling Frequency
filter_order=input('Define Filter Order : ' );
Freq_bounds=input('Choose Frequency Bounds (use brackets i.e [fmin fmax]) : ');
Filt_Signal=Filter(signal,Freq_bounds(1,1),Freq_bounds(1,2),sf,filter_order );
ph=Inst_Phase_Extraction(Filt_Signal);
where function Filter is :
[Filt_Signal]=Filter(signal,lower_limit,upper_limit,sf,filter_order )
Nyquist_freq = sf/2; % Use Of Nyquist Frequency In Order To Ensure The Validity of Shannon's Theory
lowcut = lower_limit/Nyquist_freq;
highcut = upper_limit/Nyquist_freq;
passband = [lowcut highcut]; % [fmin fmax]
[Bc Ac] = butter(filter_order, passband); % Creation Of Butterworth Filter
Filt_Signal=zeros(size(signal,1),size(signal,2));
for i=1:size(signal,1)
Filt_Signal(i,:) = filtfilt(Bc, Ac, signal(i,:));
end
end

Answers (0)

This question is closed.

Asked:

on 2 May 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!