Merged master 8748
This commit is contained in:
@@ -1,271 +0,0 @@
|
||||
program jt65sim
|
||||
|
||||
! Generate simulated JT65 data for testing WSJT-X
|
||||
|
||||
use wavhdr
|
||||
use packjt
|
||||
use options
|
||||
parameter (NMAX=54*12000) ! = 648,000
|
||||
parameter (NFFT=10*65536,NH=NFFT/2)
|
||||
type(hdr) h !Header for .wav file
|
||||
integer*2 iwave(NMAX) !Generated waveform
|
||||
integer*4 itone(126) !Channel symbols (values 0-65)
|
||||
integer dgen(12) !Twelve 6-bit data symbols
|
||||
integer sent(63) !RS(63,12) codeword
|
||||
real*4 xnoise(NMAX) !Generated random noise
|
||||
real*4 dat(NMAX) !Generated real data
|
||||
complex cdat(NMAX) !Generated complex waveform
|
||||
complex cspread(0:NFFT-1) !Complex amplitude for Rayleigh fading
|
||||
complex z
|
||||
real*8 f0,dt,twopi,phi,dphi,baud,fsample,freq,sps
|
||||
character msg*22,fname*11,csubmode*1,c,optarg*500,numbuf*32
|
||||
! character call1*5,call2*5
|
||||
logical :: display_help=.false.,seed_prngs=.true.
|
||||
type (option) :: long_options(8) = [ &
|
||||
option ('help',.false.,'h','Display this help message',''), &
|
||||
option ('sub-mode',.true.,'m','sub mode, default MODE=A','MODE'), &
|
||||
option ('num-sigs',.true.,'n','number of signals per file, default SIGNALS=10','SIGNALS'), &
|
||||
option ('doppler-spread',.true.,'d','Doppler spread, default SPREAD=0.0','SPREAD'), &
|
||||
option ('time-offset',.true.,'t','Time delta, default SECONDS=0.0','SECONDS'), &
|
||||
option ('num-files',.true.,'f','Number of files to generate, default FILES=1','FILES'), &
|
||||
option ('no-prng-seed',.false.,'p','Do not seed PRNGs (use for reproducible tests)',''), &
|
||||
option ('strength',.true.,'s','S/N in dB (2500Hz reference b/w), default SNR=0','SNR') ]
|
||||
integer nprc(126) !Sync pattern
|
||||
data nprc/1,0,0,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1,0,0, &
|
||||
0,1,0,1,1,0,0,1,0,0,0,1,1,1,0,0,1,1,1,1, &
|
||||
0,1,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1, &
|
||||
0,0,1,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1, &
|
||||
1,0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1, &
|
||||
0,1,0,1,0,0,1,1,0,0,1,0,0,1,0,0,0,0,1,1, &
|
||||
1,1,1,1,1,1/
|
||||
|
||||
! Default parameters:
|
||||
csubmode='A'
|
||||
mode65=1
|
||||
nsigs=10
|
||||
fspread=0.
|
||||
xdt=0.
|
||||
snrdb=0.
|
||||
nfiles=1
|
||||
|
||||
do
|
||||
call getopt('hm:n:d:t:f:ps:',long_options,c,optarg,narglen,nstat,noffset,nremain,.true.)
|
||||
if( nstat .ne. 0 ) then
|
||||
exit
|
||||
end if
|
||||
select case (c)
|
||||
case ('h')
|
||||
display_help = .true.
|
||||
case ('m')
|
||||
read (optarg(:narglen), *) csubmode
|
||||
if(csubmode.eq.'A') mode65=1
|
||||
if(csubmode.eq.'B') mode65=2
|
||||
if(csubmode.eq.'C') mode65=4
|
||||
case ('n')
|
||||
read (optarg(:narglen), *,err=10) nsigs
|
||||
case ('d')
|
||||
read (optarg(:narglen), *,err=10) fspread
|
||||
case ('t')
|
||||
read (optarg(:narglen), *) numbuf
|
||||
if (numbuf(1:1) == '\') then
|
||||
read (numbuf(2:), *,err=10) xdt
|
||||
else
|
||||
read (numbuf, *,err=10) xdt
|
||||
end if
|
||||
case ('f')
|
||||
read (optarg(:narglen), *,err=10) nfiles
|
||||
case ('p')
|
||||
seed_prngs=.false.
|
||||
case ('s')
|
||||
read (optarg(:narglen), *) numbuf
|
||||
if (numbuf(1:1) == '\') then
|
||||
read (numbuf(2:), *,err=10) snrdb
|
||||
else
|
||||
read (numbuf, *,err=10) snrdb
|
||||
end if
|
||||
end select
|
||||
cycle
|
||||
10 display_help=.true.
|
||||
print *, 'Optional argument format error for option -', c
|
||||
end do
|
||||
|
||||
if(display_help .or. nstat.lt.0 .or. nremain.ge.1) then
|
||||
print *, ''
|
||||
print *, 'Usage: jt65sim [OPTIONS]'
|
||||
print *, ''
|
||||
print *, ' Generate one or more simulated JT65 signals in .WAV file(s)'
|
||||
print *, ''
|
||||
print *, 'Example: jt65sim -m B -n 10 -d 0.2 -s \\-24.5 -t 0.0 -f 4'
|
||||
print *, ''
|
||||
print *, 'OPTIONS: NB Use \ (\\ on *nix shells) to escape -ve arguments'
|
||||
print *, ''
|
||||
do i = 1, size (long_options)
|
||||
call long_options(i) % print (6)
|
||||
end do
|
||||
go to 999
|
||||
endif
|
||||
|
||||
if (seed_prngs) then
|
||||
call init_random_seed() ! seed Fortran RANDOM_NUMBER generator
|
||||
call sgran() ! see C rand generator (used in gran)
|
||||
end if
|
||||
|
||||
rms=100.
|
||||
fsample=12000.d0 !Sample rate (Hz)
|
||||
dt=1.d0/fsample !Sample interval (s)
|
||||
twopi=8.d0*atan(1.d0)
|
||||
npts=54*12000 !Total samples in .wav file
|
||||
baud=11025.d0/4096.d0 !Keying rate
|
||||
sps=12000.d0/baud !Samples per symbol, at fsample=12000 Hz
|
||||
nsym=126 !Number of channel symbols
|
||||
h=default_header(12000,npts)
|
||||
dfsig=2000.0/nsigs !Freq spacing between sigs in file (Hz)
|
||||
|
||||
do ifile=1,nfiles !Loop over requested number of files
|
||||
write(fname,1002) ifile !Output filename
|
||||
1002 format('000000_',i4.4)
|
||||
open(10,file=fname//'.wav',access='stream',status='unknown')
|
||||
|
||||
xnoise=0.
|
||||
cdat=0.
|
||||
if(snrdb.lt.90) then
|
||||
do i=1,npts
|
||||
xnoise(i)=gran() !Generate gaussian noise
|
||||
enddo
|
||||
endif
|
||||
|
||||
do isig=1,nsigs !Generate requested number of sigs
|
||||
if(mod(nsigs,2).eq.0) f0=1500.0 + dfsig*(isig-0.5-nsigs/2)
|
||||
if(mod(nsigs,2).eq.1) f0=1500.0 + dfsig*(isig-(nsigs+1)/2)
|
||||
xsnr=snrdb
|
||||
if(snrdb.eq.0.0) xsnr=-19 - isig
|
||||
if(csubmode.eq.'B' .and. snrdb.eq.0.0) xsnr=-21 - isig
|
||||
if(csubmode.eq.'C' .and. snrdb.eq.0.0) xsnr=-21 - isig
|
||||
|
||||
!###
|
||||
! call1="K1ABC"
|
||||
! ic3=65+mod(isig-1,26)
|
||||
! ic2=65+mod((isig-1)/26,26)
|
||||
! ic1=65
|
||||
! call2="W9"//char(ic1)//char(ic2)//char(ic3)
|
||||
! write(msg,1010) call1,call2,nint(xsnr)
|
||||
!1010 format(a5,1x,a5,1x,i3.2)
|
||||
msg="K1ABC W9XYZ EN37"
|
||||
!###
|
||||
|
||||
call packmsg(msg,dgen,itype,.false.) !Pack message into 12 six-bit bytes
|
||||
call rs_encode(dgen,sent) !Encode using RS(63,12)
|
||||
call interleave63(sent,1) !Interleave channel symbols
|
||||
call graycode65(sent,63,1) !Apply Gray code
|
||||
|
||||
k=0
|
||||
do j=1,nsym !Insert sync and data into itone()
|
||||
if(nprc(j).eq.0) then
|
||||
k=k+1
|
||||
itone(j)=sent(k)+2
|
||||
else
|
||||
itone(j)=0
|
||||
endif
|
||||
enddo
|
||||
|
||||
bandwidth_ratio=2500.0/6000.0
|
||||
sig=sqrt(2*bandwidth_ratio)*10.0**(0.05*xsnr)
|
||||
if(xsnr.gt.90.0) sig=1.0
|
||||
write(*,1020) ifile,isig,f0,csubmode,xsnr,xdt,fspread,msg
|
||||
1020 format(i4,i4,f10.3,2x,a1,2x,f5.1,f6.2,f5.1,1x,a22)
|
||||
|
||||
phi=0.d0
|
||||
dphi=0.d0
|
||||
k=12000 + xdt*12000 !Start audio at t = xdt + 1.0 s
|
||||
isym0=-99
|
||||
do i=1,npts !Add this signal into cdat()
|
||||
isym=floor(i/sps)+1
|
||||
if(isym.gt.nsym) exit
|
||||
if(isym.ne.isym0) then
|
||||
freq=f0 + itone(isym)*baud*mode65
|
||||
dphi=twopi*freq*dt
|
||||
isym0=isym
|
||||
endif
|
||||
phi=phi + dphi
|
||||
if(phi.gt.twopi) phi=phi-twopi
|
||||
xphi=phi
|
||||
z=cmplx(cos(xphi),sin(xphi))
|
||||
k=k+1
|
||||
if(k.ge.1) cdat(k)=cdat(k) + sig*z
|
||||
enddo
|
||||
enddo
|
||||
|
||||
if(fspread.ne.0) then !Apply specified Doppler spread
|
||||
df=12000.0/nfft
|
||||
twopi=8*atan(1.0)
|
||||
cspread(0)=1.0
|
||||
cspread(NH)=0.
|
||||
|
||||
! The following options were added 3/15/2016 to make the half-power tone
|
||||
! widths equal to the requested Doppler spread. (Previously we effectively
|
||||
! used b=1.0 and Gaussian shape, which made the tones 1.665 times wider.)
|
||||
! b=2.0*sqrt(log(2.0)) !Gaussian (before 3/15/2016)
|
||||
! b=2.0 !Lorenzian 3/15 - 3/27
|
||||
b=6.0 !Lorenzian 3/28 onward
|
||||
|
||||
do i=1,NH
|
||||
f=i*df
|
||||
x=b*f/fspread
|
||||
z=0.
|
||||
a=0.
|
||||
if(x.lt.3.0) then !Cutoff beyond x=3
|
||||
! a=sqrt(exp(-x*x)) !Gaussian
|
||||
a=sqrt(1.111/(1.0+x*x)-0.1) !Lorentzian
|
||||
call random_number(r1)
|
||||
phi1=twopi*r1
|
||||
z=a*cmplx(cos(phi1),sin(phi1))
|
||||
endif
|
||||
cspread(i)=z
|
||||
z=0.
|
||||
if(x.lt.50.0) then
|
||||
call random_number(r2)
|
||||
phi2=twopi*r2
|
||||
z=a*cmplx(cos(phi2),sin(phi2))
|
||||
endif
|
||||
cspread(NFFT-i)=z
|
||||
enddo
|
||||
|
||||
do i=0,NFFT-1
|
||||
f=i*df
|
||||
if(i.gt.NH) f=(i-nfft)*df
|
||||
s=real(cspread(i))**2 + aimag(cspread(i))**2
|
||||
! write(13,3000) i,f,s,cspread(i)
|
||||
!3000 format(i5,f10.3,3f12.6)
|
||||
enddo
|
||||
! s=real(cspread(0))**2 + aimag(cspread(0))**2
|
||||
! write(13,3000) 1024,0.0,s,cspread(0)
|
||||
|
||||
call four2a(cspread,NFFT,1,1,1) !Transform to time domain
|
||||
|
||||
sum=0.
|
||||
do i=0,NFFT-1
|
||||
p=real(cspread(i))**2 + aimag(cspread(i))**2
|
||||
sum=sum+p
|
||||
enddo
|
||||
avep=sum/NFFT
|
||||
fac=sqrt(1.0/avep)
|
||||
cspread=fac*cspread !Normalize to constant avg power
|
||||
cdat=cspread(1:npts)*cdat !Apply Rayleigh fading
|
||||
|
||||
! do i=0,NFFT-1
|
||||
! p=real(cspread(i))**2 + aimag(cspread(i))**2
|
||||
! write(14,3010) i,p,cspread(i)
|
||||
!3010 format(i8,3f12.6)
|
||||
! enddo
|
||||
|
||||
endif
|
||||
|
||||
dat=aimag(cdat) + xnoise !Add the generated noise
|
||||
fac=32767.0/nsigs
|
||||
if(snrdb.ge.90.0) iwave(1:npts)=nint(fac*dat(1:npts))
|
||||
if(snrdb.lt.90.0) iwave(1:npts)=nint(rms*dat(1:npts))
|
||||
write(10) h,iwave(1:npts) !Save the .wav file
|
||||
close(10)
|
||||
enddo
|
||||
|
||||
999 end program jt65sim
|
||||
@@ -1,560 +0,0 @@
|
||||
/* MOD2DENSE-TEST. C - Program to test mod2dense module. */
|
||||
|
||||
/* Copyright (c) 1995-2012 by Radford M. Neal.
|
||||
*
|
||||
* Permission is granted for anyone to copy, use, modify, and distribute
|
||||
* these programs and accompanying documents for any purpose, provided
|
||||
* this copyright notice is retained and prominently displayed, and note
|
||||
* is made of any changes made to these programs. These programs and
|
||||
* documents are distributed without any warranty, express or implied.
|
||||
* As the programs were written for research purposes only, they have not
|
||||
* been tested to the degree that would be advisable in any important
|
||||
* application. All use of these programs is entirely at the user's own
|
||||
* risk.
|
||||
*/
|
||||
|
||||
|
||||
/* Correct output for this program is saved in the file mod2dense-test-out */
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "mod2dense.h"
|
||||
|
||||
|
||||
main(void)
|
||||
{
|
||||
mod2dense *m1, *m2, *m3, *m4;
|
||||
mod2dense *s0, *s1, *s2, *s3, *s4, *s5;
|
||||
int a_row[35], a_col[35];
|
||||
int code;
|
||||
int i, j;
|
||||
FILE *f;
|
||||
|
||||
|
||||
printf("\nPART 1:\n\n");
|
||||
|
||||
/* Set up m1 with bits on a diagonal plus a few more set to 1. */
|
||||
|
||||
m1 = mod2dense_allocate(35,40);
|
||||
|
||||
mod2dense_clear(m1);
|
||||
|
||||
for (i = 0; i<35; i++) mod2dense_set(m1,i,i,1);
|
||||
|
||||
mod2dense_set(m1,2,3,1);
|
||||
mod2dense_set(m1,34,4,1);
|
||||
mod2dense_set(m1,10,38,1);
|
||||
|
||||
/* Print m1. */
|
||||
|
||||
printf("Matrix m1:\n\n");
|
||||
mod2dense_print(stdout,m1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Store m1 in a file. */
|
||||
|
||||
f = fopen("test-file","wb");
|
||||
if (f==0)
|
||||
{ fprintf(stderr,"Can't create test-file\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!mod2dense_write(f,m1))
|
||||
{ printf("Error from mod2dense_write\n");
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
/* Read matrix written above back into m2. */
|
||||
|
||||
f = fopen("test-file","rb");
|
||||
if (f==0)
|
||||
{ fprintf(stderr,"Can't open test-file\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
m2 = mod2dense_read(f);
|
||||
|
||||
if (m2==0)
|
||||
{ printf("Error from mod2dense_read\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Print m2, along with result of equality test. */
|
||||
|
||||
printf("Matrix m2, as read from file. Should be same as m1 above.\n\n");
|
||||
mod2dense_print(stdout,m2);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Test of equality of m1 & m2 (should be 1): %d\n\n",
|
||||
mod2dense_equal(m1,m2));
|
||||
|
||||
/* Copy m1 to m3. */
|
||||
|
||||
m3 = mod2dense_allocate(mod2dense_rows(m1),mod2dense_cols(m1));
|
||||
|
||||
mod2dense_copy(m1,m3);
|
||||
|
||||
/* Print m3, along with result of equality test. */
|
||||
|
||||
printf("Matrix m3, copied from m1 above.\n\n");
|
||||
mod2dense_print(stdout,m3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Test of equality of m1 & m3 (should be 1): %d\n\n",
|
||||
mod2dense_equal(m1,m3));
|
||||
|
||||
/* Clear m3. */
|
||||
|
||||
mod2dense_clear(m3);
|
||||
|
||||
/* Print m3 again. */
|
||||
|
||||
printf("Matrix m3 again, should now be all zeros.\n\n");
|
||||
mod2dense_print(stdout,m3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Test of equality of m1 & m3 (should be 0): %d\n\n",
|
||||
mod2dense_equal(m1,m3));
|
||||
|
||||
|
||||
printf("\nPART 2:\n\n");
|
||||
|
||||
/* Compute transpose of m1. */
|
||||
|
||||
m4 = mod2dense_allocate(mod2dense_cols(m1),mod2dense_rows(m1));
|
||||
|
||||
mod2dense_transpose(m1,m4);
|
||||
|
||||
/* Print transpose. */
|
||||
|
||||
printf("Transpose of m1.\n\n");
|
||||
mod2dense_print(stdout,m4);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Free space for m1, m2, and m3. */
|
||||
|
||||
mod2dense_free(m1);
|
||||
mod2dense_free(m2);
|
||||
mod2dense_free(m3);
|
||||
|
||||
|
||||
printf("\nPART 3:\n\n");
|
||||
|
||||
/* Allocate some small matrices. */
|
||||
|
||||
s0 = mod2dense_allocate(5,7);
|
||||
s1 = mod2dense_allocate(5,7);
|
||||
s2 = mod2dense_allocate(7,4);
|
||||
s3 = mod2dense_allocate(5,4);
|
||||
s4 = mod2dense_allocate(5,7);
|
||||
|
||||
/* Set up the contents of s0, s1, and s2. */
|
||||
|
||||
mod2dense_clear(s0);
|
||||
mod2dense_clear(s1);
|
||||
mod2dense_clear(s2);
|
||||
|
||||
mod2dense_set(s0,1,3,1);
|
||||
mod2dense_set(s0,1,4,1);
|
||||
mod2dense_set(s0,2,0,1);
|
||||
mod2dense_set(s0,3,1,1);
|
||||
|
||||
mod2dense_set(s1,1,3,1);
|
||||
mod2dense_set(s1,1,5,1);
|
||||
mod2dense_set(s1,3,0,1);
|
||||
mod2dense_set(s1,3,1,1);
|
||||
mod2dense_set(s1,3,6,1);
|
||||
|
||||
mod2dense_set(s2,5,1,1);
|
||||
mod2dense_set(s2,5,2,1);
|
||||
mod2dense_set(s2,5,3,1);
|
||||
mod2dense_set(s2,0,0,1);
|
||||
mod2dense_set(s2,1,1,1);
|
||||
|
||||
/* Print s0, s1, and s2. */
|
||||
|
||||
printf("Matrix s0.\n\n");
|
||||
mod2dense_print(stdout,s0);
|
||||
printf("\nMatrix s1.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\nMatrix s2.\n\n");
|
||||
mod2dense_print(stdout,s2);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Add s0 and s1, storing the result in s4, then print s4. */
|
||||
|
||||
mod2dense_add(s0,s1,s4);
|
||||
|
||||
printf("Sum of s0 and s1.\n\n");
|
||||
mod2dense_print(stdout,s4);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Multiply s1 and s2, storing the product in s3, and then print s3. */
|
||||
|
||||
mod2dense_multiply(s1,s2,s3);
|
||||
|
||||
printf("Product of s1 and s2.\n\n");
|
||||
mod2dense_print(stdout,s3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Try clearing a bit in s3, then printing the result. */
|
||||
|
||||
mod2dense_set(s3,1,2,0);
|
||||
|
||||
printf("Above matrix with (1,2) cleared.\n\n");
|
||||
mod2dense_print(stdout,s3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Free space for s0, s1, s2, s3, and s4. */
|
||||
|
||||
mod2dense_free(s0);
|
||||
mod2dense_free(s1);
|
||||
mod2dense_free(s2);
|
||||
mod2dense_free(s3);
|
||||
mod2dense_free(s4);
|
||||
|
||||
|
||||
printf("\nPART 4:\n\n");
|
||||
|
||||
/* Set up a small square matrix, s1. Also copy it to s2. */
|
||||
|
||||
s1 = mod2dense_allocate(5,5);
|
||||
s2 = mod2dense_allocate(5,5);
|
||||
|
||||
mod2dense_clear(s1);
|
||||
|
||||
mod2dense_set(s1,0,3,1);
|
||||
mod2dense_set(s1,1,4,1);
|
||||
mod2dense_set(s1,1,1,1);
|
||||
mod2dense_set(s1,2,0,1);
|
||||
mod2dense_set(s1,3,1,1);
|
||||
mod2dense_set(s1,3,2,1);
|
||||
mod2dense_set(s1,4,2,1);
|
||||
mod2dense_set(s1,4,0,1);
|
||||
|
||||
mod2dense_copy(s1,s2);
|
||||
|
||||
/* Print s1. */
|
||||
|
||||
printf("Matrix s1.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute inverse of s1, storing it in s3. */
|
||||
|
||||
s3 = mod2dense_allocate(5,5);
|
||||
|
||||
code = mod2dense_invert(s1,s3);
|
||||
|
||||
/* Print inverse (s3). */
|
||||
|
||||
printf("Matrix s3, the inverse of s1 (return code %d).\n\n",code);
|
||||
mod2dense_print(stdout,s3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute and print product of inverse and original matrix, both ways. */
|
||||
|
||||
mod2dense_multiply(s2,s3,s1);
|
||||
printf("Original matrix times inverse (should be identity).\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
|
||||
mod2dense_multiply(s3,s2,s1);
|
||||
printf("\nInverse times original matrix (should be identity).\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute and print inverse of inverse, and do equality check. */
|
||||
|
||||
mod2dense_invert(s3,s1);
|
||||
|
||||
printf("Inverse of inverse (should be same as original s1).\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Test of equality with original (should be 1): %d\n\n",
|
||||
mod2dense_equal(s1,s2));
|
||||
|
||||
/* Free s1, s2, and s3. */
|
||||
|
||||
mod2dense_free(s1);
|
||||
mod2dense_free(s2);
|
||||
mod2dense_free(s3);
|
||||
|
||||
/* Set up a rectangular matrix like s1 above, but with two zero columns.
|
||||
Copy to s4 as well. */
|
||||
|
||||
s1 = mod2dense_allocate(5,7);
|
||||
|
||||
mod2dense_clear(s1);
|
||||
|
||||
mod2dense_set(s1,0,4,1);
|
||||
mod2dense_set(s1,1,6,1);
|
||||
mod2dense_set(s1,1,1,1);
|
||||
mod2dense_set(s1,2,0,1);
|
||||
mod2dense_set(s1,3,1,1);
|
||||
mod2dense_set(s1,3,2,1);
|
||||
mod2dense_set(s1,4,2,1);
|
||||
mod2dense_set(s1,4,0,1);
|
||||
|
||||
s4 = mod2dense_allocate(5,7);
|
||||
mod2dense_copy(s1,s4);
|
||||
|
||||
/* Print s1. */
|
||||
|
||||
printf("Matrix s1.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute inverse of sub-matrix of s1, storing it in s3. Print results. */
|
||||
|
||||
s3 = mod2dense_allocate(5,7);
|
||||
|
||||
code = mod2dense_invert_selected(s1,s3,a_row,a_col);
|
||||
|
||||
printf("Matrix s3, from invert_selected applied to s1 (return code %d).\n\n",
|
||||
code);
|
||||
|
||||
mod2dense_print(stdout,s3);
|
||||
|
||||
printf("\n row ordering returned:");
|
||||
for (i = 0; i<5; i++) printf(" %d",a_row[i]);
|
||||
printf("\n");
|
||||
|
||||
printf("\n column ordering returned:");
|
||||
for (j = 0; j<7; j++) printf(" %d",a_col[j]);
|
||||
printf("\n");
|
||||
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Columns extracted in order from original matrix.\n\n");
|
||||
|
||||
s2 = mod2dense_allocate(5,5);
|
||||
mod2dense_copycols(s4,s2,a_col);
|
||||
mod2dense_print(stdout,s2);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
s5 = mod2dense_allocate(5,5);
|
||||
code = mod2dense_invert(s2,s5);
|
||||
|
||||
printf(
|
||||
"Inverse of above calculated using mod2dense_inverse (return code %d)\n\n",
|
||||
code);
|
||||
|
||||
mod2dense_print(stdout,s5);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf(
|
||||
"Columns extracted in order from s3 (should also be inverse of above).\n\n");
|
||||
mod2dense_copycols(s3,s2,a_col);
|
||||
mod2dense_print(stdout,s2);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Try out mod2dense_invert_selected again. */
|
||||
|
||||
mod2dense_clear(s1);
|
||||
|
||||
mod2dense_set(s1,0,0,1);
|
||||
mod2dense_set(s1,0,1,1);
|
||||
mod2dense_set(s1,1,1,1);
|
||||
mod2dense_set(s1,1,2,1);
|
||||
mod2dense_set(s1,2,0,1);
|
||||
mod2dense_set(s1,2,2,1);
|
||||
mod2dense_set(s1,3,3,1);
|
||||
mod2dense_set(s1,3,4,1);
|
||||
|
||||
printf("Matrix s1.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
code = mod2dense_invert_selected(s1,s3,a_row,a_col);
|
||||
|
||||
printf("Matrix s3, from invert_selected applied to s1 (return code %d).\n\n",
|
||||
code);
|
||||
|
||||
mod2dense_print(stdout,s3);
|
||||
|
||||
printf("\n row ordering returned:");
|
||||
for (i = 0; i<5; i++) printf(" %d",a_row[i]);
|
||||
printf("\n");
|
||||
printf("\n column ordering returned:");
|
||||
for (j = 0; j<7; j++) printf(" %d",a_col[j]);
|
||||
printf("\n");
|
||||
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Matrix s4, from copying rows in order from s3.\n\n");
|
||||
|
||||
mod2dense_copyrows(s3,s4,a_row);
|
||||
|
||||
mod2dense_print(stdout,s4);
|
||||
|
||||
free(s1);
|
||||
free(s2);
|
||||
free(s3);
|
||||
free(s4);
|
||||
free(s5);
|
||||
|
||||
|
||||
printf("\nPART 5:\n\n");
|
||||
|
||||
/* Set up a larger square matrix, s1. Also copy it to s2. */
|
||||
|
||||
s1 = mod2dense_allocate(35,35);
|
||||
s2 = mod2dense_allocate(35,35);
|
||||
|
||||
mod2dense_clear(s1);
|
||||
|
||||
for (i = 0; i<35; i++) mod2dense_set(s1,i,i,1);
|
||||
|
||||
mod2dense_set(s1,10,3,1);
|
||||
mod2dense_set(s1,11,4,1);
|
||||
mod2dense_set(s1,11,11,1);
|
||||
mod2dense_set(s1,12,20,1);
|
||||
mod2dense_set(s1,13,31,1);
|
||||
mod2dense_set(s1,23,12,1);
|
||||
mod2dense_set(s1,24,12,1);
|
||||
mod2dense_set(s1,14,10,1);
|
||||
mod2dense_set(s1,2,20,1);
|
||||
mod2dense_set(s1,3,31,1);
|
||||
mod2dense_set(s1,3,12,1);
|
||||
mod2dense_set(s1,24,2,1);
|
||||
mod2dense_set(s1,24,0,1);
|
||||
mod2dense_set(s1,5,3,1);
|
||||
mod2dense_set(s1,18,3,1);
|
||||
mod2dense_set(s1,17,11,1);
|
||||
mod2dense_set(s1,32,23,1);
|
||||
mod2dense_set(s1,9,24,1);
|
||||
mod2dense_set(s1,19,11,1);
|
||||
mod2dense_set(s1,11,30,1);
|
||||
mod2dense_set(s1,21,27,1);
|
||||
mod2dense_set(s1,21,22,1);
|
||||
mod2dense_set(s1,23,33,1);
|
||||
mod2dense_set(s1,24,23,1);
|
||||
mod2dense_set(s1,24,25,1);
|
||||
mod2dense_set(s1,30,34,1);
|
||||
mod2dense_set(s1,31,10,1);
|
||||
mod2dense_set(s1,33,17,1);
|
||||
mod2dense_set(s1,33,18,1);
|
||||
mod2dense_set(s1,34,8,1);
|
||||
mod2dense_set(s1,34,11,1);
|
||||
mod2dense_set(s1,34,3,1);
|
||||
mod2dense_set(s1,34,24,1);
|
||||
mod2dense_set(s1,25,34,1);
|
||||
mod2dense_set(s1,13,34,1);
|
||||
|
||||
mod2dense_set(s1,3,3,0);
|
||||
mod2dense_set(s1,11,11,0);
|
||||
mod2dense_set(s1,23,23,0);
|
||||
mod2dense_set(s1,24,24,0);
|
||||
|
||||
mod2dense_copy(s1,s2);
|
||||
|
||||
/* Print s1. */
|
||||
|
||||
printf("Matrix s1.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute inverse of s1, storing it in s3. */
|
||||
|
||||
s3 = mod2dense_allocate(35,35);
|
||||
|
||||
code = mod2dense_invert(s1,s3);
|
||||
|
||||
/* Print inverse (s3). */
|
||||
|
||||
printf("Matrix s3, the inverse of s1 (return code %d).\n\n",code);
|
||||
mod2dense_print(stdout,s3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute and print product of inverse and original matrix, both ways. */
|
||||
|
||||
mod2dense_multiply(s2,s3,s1);
|
||||
printf("Original matrix times inverse (should be identity).\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
|
||||
mod2dense_multiply(s3,s2,s1);
|
||||
printf("\nInverse times original matrix (should be identity).\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute and print inverse of inverse, and do equality check. */
|
||||
|
||||
mod2dense_invert(s3,s1);
|
||||
|
||||
printf("Inverse of inverse (should be same as original s1).\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Test of equality with original (should be 1): %d\n\n",
|
||||
mod2dense_equal(s1,s2));
|
||||
|
||||
/* Free s1, s2, and s3. */
|
||||
|
||||
mod2dense_free(s1);
|
||||
mod2dense_free(s2);
|
||||
mod2dense_free(s3);
|
||||
|
||||
|
||||
printf("\nPART 6:\n\n");
|
||||
|
||||
/* Set up a largish square matrix, s1. Also copy it to s2. */
|
||||
|
||||
s1 = mod2dense_allocate(35,35);
|
||||
s2 = mod2dense_allocate(35,35);
|
||||
|
||||
mod2dense_clear(s1);
|
||||
|
||||
for (i = 0; i<10; i++)
|
||||
{ if (i!=3 && i!=7)
|
||||
{ mod2dense_set(s1,i,i,1);
|
||||
}
|
||||
}
|
||||
for (i = 10; i<35; i++)
|
||||
{ if (i!=15 && i!=21 && i!=32)
|
||||
{ mod2dense_set(s1,i,34-(i-10),1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print s1. */
|
||||
|
||||
printf("Matrix s1.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Forcibly invert s1, storing inverse in s3. */
|
||||
|
||||
s3 = mod2dense_allocate(35,35);
|
||||
|
||||
code = mod2dense_forcibly_invert(s1,s3,a_row,a_col);
|
||||
|
||||
/* Print inverse, and list of altered elements. */
|
||||
|
||||
printf("Result of forcibly inverting s1 (needed to alter %d elements).\n\n",
|
||||
code);
|
||||
mod2dense_print(stdout,s3);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("Altered elements at these indexes:\n\n");
|
||||
|
||||
for (i = 0; i<code; i++)
|
||||
{ printf("%3d %3d\n",a_row[i],a_col[i]);
|
||||
}
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
/* Compute and print inverse of inverse. */
|
||||
|
||||
mod2dense_invert(s3,s1);
|
||||
|
||||
printf("Inverse of inverse of altered matrix.\n\n");
|
||||
mod2dense_print(stdout,s1);
|
||||
printf("\n"); fflush(stdout);
|
||||
|
||||
printf("\nDONE WITH TESTS.\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user