0
$\begingroup$

This code supposed to generate random frequency response which could be used as impulse...But it fails to pack correctly when going from fft domain to real signal time domain, the other half is left inside the real signal...

clear all,
close all,



mn = 37.2700     %minimum amplitude value in dB
mx = 140.3640    %maximum amplitude value in dB
N = 44100        %length of the impulse

ampr = mn + (mx+mx)*rand(N,1) %random values for amplitude

a=db2mag(ampr)        %convert dB to magnitude
a1=a(2:end)
a2 = flipud(a(2:end))          %create a symetric mirror for negative frequencies
a3=[a1;  a2]            %combine positive and negative frequency amplitude arrays           


pmn =-180        %minimum phase
pmx = 180        %maximum phase




phaser = pmn + (pmx+pmx)*rand(N,1)   %random phase values

ph=wrapToPi(deg2rad(phaser)) %convert phase degrees to radians values between pi and -pi
ph1=ph(2:end)                            
ph2 = flipud(-ph(2:end))    %flip and create mirror phase values for negative frequencies
ph3=[ph1;  ph2]                %combine two mirror symetric halves of positive frequency and negative frequency phase arrays

mk2=real(ifft(pol2cart(ph3,a3)))  %convert polar values to cartesian and finally to real time domain signal
fs=44100                          %assign sampling rate
% mk2 = mk2(1:floor(length(mk2)/2)) %remove the other half caused by negative frequencies
mk2=mk2/max(max(abs(mk2)))       %normalize the signal         


audiowrite('RandomGeneratedImpulse.wav',mk2,fs,'BitsPerSample',24)
$\endgroup$
  • $\begingroup$ I'm pretty sure this had been asked before, but I can't find the previous question... $\endgroup$ – MBaz Sep 2 '16 at 16:30
0
$\begingroup$

Your code looks good and functional, the only thing you need to do is to add DC values for both negative and positive frequencies...Make sure you skip the DC component for negative frequencies when working with odd number of samples...

clear all,
close all,

mn = 37.2700     %minimum amplitude value in dB
mx = 140.3640    %maximum amplitude value in dB
N = 44100        %length of the impulse

ampr = mn + (mx+mx)*rand(N,1) %random values for amplitude

a=db2mag(ampr)        %convert dB to magnitude
a2=flipud(a)          %create a symmetric mirror for negative frequencies
a3=[a; a2]            %combine positive and negative frequency amplitude arrays           

pmn =-180        %minimum phase
pmx = 180        %maximum phase

phaser = pmn + (pmx+pmx)*rand(N,1)   %random phase values

ph=wrapToPi(deg2rad(phaser)) %convert phase degrees to radians values between pi and -pi
ph2=flipud(ph)*-1            %flip and create mirror phase values for negative frequencies                 

ph3=[ph; ph2]                %combine two mirror symmetric halves of positive frequency and negative frequency phase arrays

mk2=real(ifft(pol2cart(ph3,a3)))  %convert polar values to cartesian and finally to real time domain signal
fs=44100                          %assign sampling rate
mk2 = mk2(1:floor(length(mk2)/2)) %remove the other half caused by negative frequencies
mk2=mk2/max(max(abs(mk2)))       %normalize the signal         

audiowrite('RandomGeneratedImpulse.wav',mk2,fs,'BitsPerSample',24)
$\endgroup$
  • $\begingroup$ For symmetry, you also need to NOT put in the DC term of a into a3, regardless of the length parity. I think the a2 expression should be a2 = flipud(a(2:end));. Same with ph2 : ph2 = flipud(-ph(2:end));. Unless you don't want symmetry in the frequency response. -1 $\endgroup$ – Peter K. Sep 2 '16 at 18:24
  • $\begingroup$ Peter K. you are absolutely right, this where the code fails, I believe now the impulse should be all real signal and there shouldn't be any excess of complex values caused of negative frequencies...I have updated the code with this information in mind but still can't get a clean all real signal...there is still other half of the impulse left in the packed signal in time domain. $\endgroup$ – skyisfalling Sep 3 '16 at 14:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.