# Generate impulse from random frequency response [closed]

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

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)

• I'm pretty sure this had been asked before, but I can't find the previous question... – MBaz Sep 2 '16 at 16:30

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


• 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 – Peter K. Sep 2 '16 at 18:24